VBA to Add / Delete Watermark on Each Page

I am programming a form that will allow us to print out the current document while (1) adding a Watermark (which must appear on EVERY page) and then (2) removing that watermark.  My code now only puts the watermark on the first page.

Also, is there a way to use a pre-done (but non-Word standard) watermark so that I don't have to recreate it each time on the fly?

How is that done?

TIA
Answer
Answer
In order to appear on every page, the watermark must be in the header/footer range of each page. There would be up to three header footer ranges for each section of the document. In order to determine how best to achieve this we would need to know a number of things e.g.

Is the form a protected form?
Is there anything already in the header or footer of the pages in question, if so what?
What type of watermark do you wish to add?

The simplest approach using a macro would be to save the document, add the watermark to the appropriate header or footer ranges, print the document then close without saving the changes and finally re-open the document. Something like the following should work for most circumstances. You need only change the password and the building block name as appropriate. I have annotated the code.

The macro below uses a standard building block. If you want to use your own building block with the pre-configured watermark, then you will need to change the path in "strBBPath". If you have not already done so insert the watermark in a blank document created from the form template and save it as a building block in the form document template.

Sub PrintFormWithWMark()
Dim oDoc As Document
Dim oSection As Section
Dim oHeader As HeaderFooter
Dim oRng As Range
Dim strName As String
Dim strBBPath As String
Dim bProtected As Boolean
Const strPassword As String = "" 'Password to unlock protected form
Const strBBName As String = "CONFIDENTIAL 1" 'The building block name that you want to insert

strBBPath = "C:\Users\" & (Environ$("Username")) & "\AppData\Roaming\Microsoft\Document Building Blocks\1033\14\Built-In Building Blocks.dotx"

Set oDoc = ActiveDocument
oDoc.Save 'save the document
strName = oDoc.FullName 'Record the document name

'Unprotect the file if protected
If ActiveDocument.ProtectionType <> wdNoProtection Then
    bProtected = True
    ActiveDocument.Unprotect Password:=strPassword
End If

'Address each section
For Each oSection In oDoc.Sections
    'Address each header in the section
    For Each oHeader In oSection.Headers
        Set oRng = oHeader.Range
        oRng.Start = oRng.End 'set the range to the end of the header
        'Insert the built-in building block
        Application.Templates(strBBPath).BuildingBlockEntries(strBBName).Insert Where:=oRng, RichText:=True
    Next oHeader
Next oSection

'If thedocument  was protected - reprotect it
If bProtected = True Then
    ActiveDocument.Protect _
    Type:=wdAllowOnlyFormFields, _
    NoReset:=True, _
    Password:=strPassword
End If

oDoc.PrintOut 'Print to the current printer
oDoc.Close wdDoNotSaveChanges 'Close without saving the watermark
Documents.Open strName 'Reopen the document without the watermark
MsgBox "Document sent to printer." 'optional
End Sub


Graham Mayor (Microsoft Word MVP 2002-2019)
For more Word tips and downloads visit my web site
https://www.gmayor.com/Word_pages.htm

1 person found this reply helpful

·

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated October 5, 2021 Views 7,170 Applies to: