1

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?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Eddy
  • 105
  • 4
  • 15

3 Answers3

2

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
Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
  • Thanks for your help! I will try it and let you know! – Eddy Jul 17 '12 at 04:14
  • @Siddarth Rout, Hi Siddarth, I tried the above mentionned bookmark technique but unfortunatly it doesn't seem to capture the event of printing. the "hasprint" function is still false no matter where I print it from (toolbar quick print icon, or File-Print). I suspect I must have somewhere along the line copy the codes into the wrong modules. Does it matter whether the modules are at "Normal" , or at the "Current" project level? – Eddy Jul 18 '12 at 17:17
  • Can you show me the word document that you created? If you have done it like I mentioned above then there is one more thing which you might have missed... `Close your document and reopen it. Now test it.` – Siddharth Rout Jul 18 '12 at 17:48
  • @siddarth Yes I included the Document_Close() sub , I placed it right in the module below hasPrinted function. What would be the best way to show you the document I have working in progress? – Eddy Jul 18 '12 at 22:03
  • @Siddarth hi Siddarth, here it is http://wikisend.com/download/603636/Testing Word Document.doc – Eddy Jul 18 '12 at 23:51
  • Test this for me. Does it work now? http://wikisend.com/download/525112/Testing Word Document.doc – Siddharth Rout Jul 19 '12 at 00:19
  • @Siddarth Its working now! :) was the issue in the "On Error Resume Next" that you placed in your Private Sub Document_Close() , and you changed the name from Private Sub Autoexec() to Private Sub Document_Open()? – Eddy Jul 19 '12 at 23:12
  • Yes. I made some changes. Yes that OERN will remain as it is :) – Siddharth Rout Jul 20 '12 at 00:52
1

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.

Community
  • 1
  • 1
Daniel
  • 12,982
  • 3
  • 36
  • 60
0

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
Oscar Sun
  • 1,427
  • 2
  • 8
  • 13