MSO 2003 xpp sp3
Hi
I am wanting to have a macro run in Word to create a header and insert specific text when File/Print is selected.
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Run macro when print selected
I am wanting to have a macro run in Word to create a header and insert specific text when File/Print is selected.
You can intercept printing by naming a macro FilePrint. There are some fine points to it, but generally speaking, you can display the dialog, capture the user’s instructions, update the document, then execute the user’s instructions. (You may also need to intercept the FilePrintDefault toolbar button — printing that bypasses the Print dialog).
Regarding the changes you want to make, it’s always a little tricky not ruining the existing contents of a header or footer when editing them in code. Or did you mean you want to insert some content at the beginning of the “body” of the document? Can you describe your requirements in a little more detail? You could, for example, upload “before and after” documents showing what the code should do.
Jefferson hi
I have managed to find some code to do what I want. WE are not worried about other content in the header as there should not be any.
FYI the code I have used
In the project explorer “ThisDocument” I have put the code below into the code window.
Private Sub Document_Open() Call Register_Event_Handler End Sub
Inserted a new blank module and inserted the code below
Dim X As New Class1 Public Sub Register_Event_Handler() Set X.App = Word.Application End Sub Inserted a Class Module and inserted the code below. Public WithEvents App As Word.Application Private Sub App_DocumentBeforePrint(ByVal Doc As Document, Cancel As Boolean) If ActiveWindow.View.SplitSpecial wdPaneNone Then ActiveWindow.Panes(2).Close End If If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _ ActivePane.View.Type = wdOutlineView Then ActiveWindow.ActivePane.View.Type = wdPrintView End If ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader Selection.Font.Size = 8 Selection.TypeText Text:= _ "Document uncontrolled when printed: Printed on: " Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _ "PRINTDATE @ ""dddd, d MMMM yyyy"" ", PreserveFormatting:=True Selection.TypeText Text:=", " Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _ "PRINTDATE @ ""h:mm am/pm"" ", PreserveFormatting:=True ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument Application.PrintOut FileName:="", Range:=wdPrintAllDocument, Item:= _ wdPrintDocumentContent, Copies:=1, Pages:="", PageType:=wdPrintAllPages, _ ManualDuplexPrint:=False, Collate:=True, Background:=True, PrintToFile:= _ False, PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, _ PrintZoomPaperHeight:=0 End Sub
For some reason it prints 2 copies of the test document!
Thanks for replying though
This is definitely not the simplest approach, but it might be best in your case.
For some reason it prints 2 copies of the test document!
Your code contains this method: Application.PrintOut. However, you are not settings Cancel = True, so Word goes ahead and prints following the user’s instructions in the Print dialog.
I think the best solution is to comment out Application.PrintOut and just let Word print normally, especially if the user might be printing only a few pages of the document or even a selection.
What happens on the second print? Does the header get replaced or does the content get doubled?
This is definitely not the simplest approach, but it might be best in your case.
Your code contains this method: Application.PrintOut. However, you are not settings Cancel = True, so Word goes ahead and prints following the user’s instructions in the Print dialog.
I think the best solution is to comment out Application.PrintOut and just let Word print normally, especially if the user might be printing only a few pages of the document or even a selection.
What happens on the second print? Does the header get replaced or does the content get doubled?
Jefferson you’re right I commentdout that line of code. It just printed 2 copies
hi Bonriki,
Why don’t you simply add the necessary text & field coding to the document (and it’s template, so that future documents get created with it)?
Paul hi
Mostly the document will be viewed on the internet, go here, Promapp, and hover over the Definitions folder and click the link. This shows the document but we need to show that it will be uncontrolled if printed.
Paul hi
Mostly the document will be viewed on the internet, go here, Promapp, and hover over the Definitions folder and click the link. This shows the document but we need to show that it will be uncontrolled if printed.
Hi Phil,
That’s OK as far as it goes, but why not incorporate the info in the standard footer, where the alert can be seen before the document is printed? Alternatively, if you only want the message to appear on printed versions, use either of the following field codes in the header:
{IF{PRINTDATE @ yyyyMMddHHmm}= {DATE @ yyyyMMddHHmm} “Document uncontrolled when printed. Printed on: {PRINTDATE @ “DDDD, D MMMM YYYY, hh:mm am/pm”}”}
or:
{IF{PRINTDATE @ yyyy}> 0 “Document uncontrolled when printed. Printed on: {PRINTDATE @ “DDDD, D MMMM YYYY, hh:mm am/pm”}”}
The first version generates a field that displays on the electronic file for up to a minute after printing, after which a field update will hide it again; the second version leaves a permanent message on the electronic file. In either case, it seems to me that it would be preferable to move the ‘Document uncontrolled when printed.’ outside the field code, so that it always appears in the header.
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Hi Phil,
That’s OK as far as it goes, but why not incorporate the info in the standard footer, where the alert can be seen before the document is printed? Alternatively, if you only want the message to appear on printed versions, use either of the following field codes in the header:
{IF{PRINTDATE @ yyyyMMddHHmm}= {DATE @ yyyyMMddHHmm} “Document uncontrolled when printed. Printed on: {PRINTDATE @ “DDDD, D MMMM YYYY, hh:mm am/pm”}”}
or:
{IF{PRINTDATE @ yyyy}> 0 “Document uncontrolled when printed. Printed on: {PRINTDATE @ “DDDD, D MMMM YYYY, hh:mm am/pm”}”}
The first version generates a field that displays on the electronic file for up to a minute after printing, after which a field update will hide it again; the second version leaves a permanent message on the electronic file. In either case, it seems to me that it would be preferable to move the ‘Document uncontrolled when printed.’ outside the field code, so that it always appears in the header.
Hi again Paul
I think it is OK as it is. The nature of the software is that it will not save altered documents back to the library where they were extracted from.
If the documents require amending they will need to be uploaded again
Hi again Paul
I think it is OK as it is. The nature of the software is that it will not save altered documents back to the library where they were extracted from.
If the documents require amending they will need to be uploaded again
Hi Phil,
But doesn’t the same risk exist if the user simply downloads the document from the library (eg via File|Save As)? If they then work from their saved copy, regardless of whether the document gets printed, surely the same risk exists? Also, for the macro solution to work, you need to persuade the user to enable macros for these documents – with field coding, there’s nothing that relies on the user’s response.
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Hi Phil,
But doesn’t the same risk exist if the user simply downloads the document from the library (eg via File|Save As)? If they then work from their saved copy, regardless of whether the document gets printed, surely the same risk exists? Also, for the macro solution to work, you need to persuade the user to enable macros for these documents – with field coding, there’s nothing that relies on the user’s response.
Paul hi
Your right there is risk that users may use File|Save As but we seem to have convinced our users not to do that. All the users computers have a group template, similar to normal.dot, that is sets the parameters for Word to run under including setting the secuurity level to Medium. We have many macro driven forms letters etc so they are all conditioned to hit Enable Macros.
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.
Notifications