简体   繁体   中英

Word- VBA- How To Automatically Run Code Before Printing?

Document has shading wdColorGray25 by default. I want to be able to remove all shading before printing (or saving as a PDF) but this doesn't seem to work as simple as it would in Excel (the code works fine by itself in a standard module).

Private Sub appWord_DocumentBeforePrint(ByVal Doc As Document, Cancel As Boolean)
    With ActiveDocument.Styles("example").Font
        With .Shading
            .BackgroundPatternColor = wdColorAutomatic
        End With
    End With
End Sub

In other words, if I hit Ctrl+P , the document will remove the shading before printing and then apply it back after printing. Would also like the change to be reflected in the print preview as well.

If I select the FilePrint command and hit Create it generates the following code (not Application.PrintOut ): 在此处输入图片说明

Sub FilePrint()
'
' FilePrint Macro
' Prints the active document
'
    Dialogs(wdDialogFilePrint).Show

End Sub

If I modify the code to:

Sub PrintOut()
' PrintOut runs when you use Ctrl + P

    With ActiveDocument.Styles("example").Font
        With .Shading
            .BackgroundPatternColor = wdColorAutomatic
        End With
    End With

    Application.PrintOut
    
    With ActiveDocument.Styles("example").Font
        With .Shading
            .BackgroundPatternColor = wdColorGray25
        End With
    End With
    

End Sub

And then hit Ctrl+P , you can see from the below screenshot below that the text shading still appears. 在此处输入图片说明

However, if I change the name of the sub like below, it will print without the shading but the print preview pane does not appear (it just prints directly without any prompts in between).

Sub PrintPreviewandPrint()
' PrintOut runs when you use Ctrl + P

    With ActiveDocument.Styles("example").Font
        With .Shading
            .BackgroundPatternColor = wdColorAutomatic
        End With
    End With

    Application.PrintOut
    
    With ActiveDocument.Styles("example").Font
        With .Shading
            .BackgroundPatternColor = wdColorGray25
        End With
    End With
    

End Sub

In Word, you can capture the built-in commands to modify them. To see how this works, try these steps:

  1. In Word, choose Developer>Macros .
  2. Change the Macros in dropdown to Word commands .
  3. Find and select FilePrint .
  4. Change the Macros in dropdown back to Normal.dotm .
  5. Click on the Create button. Word creates a new macro titled with the command name.
Sub PrintOut()
' PrintOut runs when you use Ctrl + P

    With ActiveDocument.Styles("example").Font
        With .Shading
            .BackgroundPatternColor = wdColorAutomatic
        End With
    End With

    Application.PrintOut
    
    With ActiveDocument.Styles("example").Font
        With .Shading
            .BackgroundPatternColor = wdColorGray25
        End With
    End With
    

End Sub
Sub FilePrint()
'
' FilePrint Macro
' Prints the active document
'

    With ActiveDocument.Styles("example").Font
        With .Shading
            .BackgroundPatternColor = wdColorAutomatic
        End With
    End With

    Dialogs(wdDialogFilePrint).Show

    With ActiveDocument.Styles("example").Font
        With .Shading
            .BackgroundPatternColor = wdColorGray25
        End With
    End With


End Sub

Now add your code to modify the style before and after the Dialogs line, removing the shading before and adding it after.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM