The Anchorage
Personal website of Gregory K. Maxey, Commander USN (Retired)
The information, illustrations and code contained in my "Microsoft Word Tips" are provided free and without risk or obligation.
However, the work is mine. If you use it for commercial purposes or benefit from my efforts through income earned or time saved then a donation, however small, will help to ensure the continued availability of this resource.
If you would like to donate, please use the appropriate donate button to access PayPal. Thank you!
This Microsoft Word Help & Tips page was inspired by a Microsoft Public news group question. The questioner wanted to know how to automatically add a temporary "Copy" watermark to a saved document whenever that document is reopened and reprinted.
In addition to answering the basic question, I will also show you how to intercept the various "Print" events pre-Word 2010 and how to work with a shape object in the document header.
For this discussion, I will be using a simple invoice document. The associated template contains all the VBA procedures needed to add a temporary watermark to the header, print the document, and then remove the watermark. The template used in this discussion is provided for in .zip format. You can download it here: Custom Invoice.zip.
Note: This tips page and template was developed for Word 2003. It is wholly functional with Word 2007. It has not been tested or employed with Word 2010, since known changes in the application user interface (e.g., Backstage view) and the drawing layer make it unlikely that it would perform reliably.
The illustration above shows how the basic document looks when it is printed the first time and whenever it is open on the screen. Then illustration below shows how the document appears when re-printed.
Note: The watermark is only added following the first complete printing operation. It is not added to any second or other subsequent copy if the Print Dialog is used to print multiple copies during the first print operation.
The VBA code for this process is discussed and provided below.
Option Explicit Private bRePrint As Boolean Sub AutoOpen() 'No "Printed" variable will throw a run-time error On Error GoTo Err_Handler 'Check stored variable to determine if document has been printed previously. If ActiveDocument.Variables("Printed").Value = "True" Then bRePrint = True Else bRePrint = False End If Exit Sub Err_Handler: 'Create a "Printed" variable and set value to false. ActiveDocument.Variables("Printed").Value = "False" Resume End Sub Sub FilePrint() 'Intercepts File > Print (Ctrl+P), (Print dialog) If bRePrint Then 'Call procedure that inserts watermark InsertCOPYWatermark 'Show print dialog Dialogs(wdDialogFilePrint).Show On Error Resume Next 'Call procedure to delete watermark DeleteCopyWaterMark On Error GoTo 0 Else Dialogs(wdDialogFilePrint).Show 'Set value of "Printed" variable ActiveDocument.Variables("Printed").Value = "True" ActiveDocument.Save 'Call AutoOpen to reset bRePrint value. AutoOpen End If lbl_Exit: Exit Sub End Sub Sub FilePrintDefault() 'Intercepts Print button If bRePrint Then 'Call procedure that inserts watermark InsertCOPYWatermark ActiveDocument.PrintOut Background:=True On Error Resume Next 'Call procedure to delete watermark DeleteCopyWaterMark On Error GoTo 0 Else ActiveDocument.PrintOut Background:=True 'Set value of "Printed" variable ActiveDocument.Variables("Printed").Value = "True" ActiveDocument.Save 'Call AutoOpen to reset bRePrint value. AutoOpen End If lbl_Exit: Exit Sub End Sub Sub InsertCOPYWatermark() Dim oHdr As HeaderFooter Dim oShape As Word.Shape Set oHdr = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary) 'Add WordArt shape Set oShape = oHdr.Shapes.AddTextEffect(msoTextEffect1, _ "COPY", "Times New Roman", 1, False, False, 0, 0) 'Customize oShape With oShape .Name = "Copy Watermark" .TextEffect.NormalizedHeight = False .Line.Visible = False .Fill.Visible = True .Fill.Solid .Fill.ForeColor.RGB = RGB(192, 192, 192) .Fill.Transparency = 0.5 .Rotation = 315 .LockAspectRatio = True .Height = InchesToPoints(3) .Width = InchesToPoints(4.5) .WrapFormat.AllowOverlap = True .WrapFormat.Side = wdWrapNone .WrapFormat.Type = 3 .RelativeHorizontalPosition = wdRelativeVerticalPositionMargin .RelativeVerticalPosition = wdRelativeVerticalPositionMargin .Left = wdShapeCenter .Top = wdShapeCenter End With End Sub Sub DeleteCopyWaterMark() Dim oHdr As HeaderFooter Dim oShape As Word.Shape Set oHdr = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary) Set oShape = oHdr.Shapes(1) oShape.Delete lbl_Exit: Exit Sub End Sub Sub FilePrintPreview() If bRePrint Then ActiveDocument.PrintPreview InsertCOPYWatermark Else ActiveDocument.PrintPreview End If lbl_Exit: Exit Sub End Sub Sub ClosePreview() ActiveDocument.ClosePrintPreview On Error Resume Next DeleteCopyWaterMark On Error GoTo 0 lbl_Exit: Exit Sub End Sub
See: Installing Macros for instructions on how to set up and use the macros in this tips page.
This concludes this Microsoft Word Help & Tips page. I hope you have found this page useful and informative.
The information, illustrations and code contained in my "Microsoft Word Tips" are provided free and without risk or obligation.
However, the work is mine. If you use it for commercial purposes or benefit from my efforts through income earned or time saved then a donation, however small, will help to ensure the continued availability of this resource.
If you would like to donate, please use the appropriate donate button to access PayPal. Thank you!