Eddy
Eddy

Reputation: 105

how to check if word document has printed already

I want to enable another function "code" only if the document has been printed already, i was thinking something along the lines of

Sub Testing
    Dim hasPrinted as boolean
    If ActiveDocument.PrintOut = True Then
        hasPrinted = True
        call code here...
    Else
        hasPrinted = False
        MsgBox "Please Print Before Adding"
    End If
End Sub

i receive an error that says "Compile Error, expected function or variable" on the "ActiveDocument.PrintOut" line. Could anyone give me some directions?

Upvotes: 1

Views: 3996

Answers (3)

Oscar  Sun
Oscar Sun

Reputation: 1479

How about this? https://stackoverflow.com/a/76738547/8249058 Use BackgroundPrintingCount property to implement.

Sub Testing()
    Dim hasPrinted As Boolean, BackgroundPrintingCount As Integer
    BackgroundPrintingCount = Word.Application.BackgroundPrintingStatus
    ActiveDocument.PrintOut
    While Word.Application.BackgroundPrintingStatus > BackgroundPrintingCount
        hasPrinted = False
    Wend
    hasPrinted = True
    
    
    If hasPrinted = True Then
        'call code here...
    Else
        hasPrinted = False
        MsgBox "Please Print Before Adding"
    End If
End Sub

Upvotes: 0

Daniel
Daniel

Reputation: 13122

This question provides information about creating a make-shift Document After Print event.

Once you've done that, you can have a boolean value updated to true to indicate the document has printed. Word does not store this information natively.

Upvotes: 1

Siddharth Rout
Siddharth Rout

Reputation: 149295

Capturing the print events is not an easy job in Word VBA. However here is a neat trick :)

For this do the following

Create a class module say Class1 and paste this code

Option Explicit

Public WithEvents oApp As Word.Application

Private Sub oApp_DocumentBeforePrint(ByVal Doc As Document, Cancel As Boolean)
    ActiveDocument.Bookmarks("DocWasPrinted").Delete
    With ActiveDocument.Bookmarks
        .Add Range:=Selection.Range, Name:="DocWasPrinted"
        .DefaultSorting = wdSortByName
        .ShowHidden = True
    End With
End Sub

Now insert a module and paste this code

Option Explicit

Dim oAppClass As New Class1

Public Sub AutoExec()
    Set oAppClass.oApp = Word.Application
End Sub

Sub Testing()
    If hasPrinted = True Then
        MsgBox "Document was printed"
        '~~> Call your code
    Else
        MsgBox "Please Print Before Adding"
    End If
End Sub

Function hasPrinted() As Boolean
    If ActiveDocument.Bookmarks.Exists("DocWasPrinted") = True Then
        hasPrinted = True
    End If
End Function

Close your document and reopen it. Now test it.

LOGIC:

What this code does is the moment the user prints the document, the code creates a hidden bookmark called DocWasPrinted And in my code I check if the bookmark was created or not.

Remember to delete the bookmark on Document Exit.

Private Sub Document_Close()
     ActiveDocument.Bookmarks("DoWasPrinted").Delete
End Sub

Upvotes: 2

Related Questions