Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Run macro before printing word document. 2

Status
Not open for further replies.

alan1203

Technical User
Mar 16, 2007
27
GB
Hi all.
My problem is probably very simple to solve but as i'm new to this sort of thing i'm unable to work it out.
I have a document that I need to print 3 times, each with a different watermark on them. I have recorded some macros to insert the watermark then delete it to be replace with the next one. The code runs fine when run from Visual Basic Editor but when I print the word doc using the print button in the toolbar it just prints 1 page with no watermark. This is what my code looks like:

Private Sub Document_Print()
Macro1
ActiveDocument.Printout, , , , , , , 1
Macro2
Macro3
ActiveDocument.Printout , , , , , , , 1
Macro2
Macro4
ActiveDocument.Printout, , , , , , , 1
Macro2
End Sub

Macro 1,3 and 4 is putting in the watermark and Macro 2 is removing it.
Any help is really appreciated.
 
There are TWO Word commands to print.

Sub DocumentPrint() - is fired by File > Print

Sub DocumentPrintDefault() - is fired by the Print button.

TWO separate commands. Clicking the print button is not firing your code.

Gerry
My paintings and sculpture
 
Thanks for the replies guys but I still can't get any of your suggestions to work. My page is still just printing the once and without any watermarks. Is it possible that you could give me the exact code that I should be putting in?
 
I believe what may be more relevant is YOU posting your code. We have no idea what your macro 1, and macro 2 is doing...and it is there the problem lies, yes?

Gerry
My paintings and sculpture
 
Well if you read my first post completely it does say that macro 1,3 and 4 are inserting a watermark and macro 2 is deleating it!!. These are just macros i've recorded using the record macro feature in word. All I want to know is how to get them to run when I print the document.
Any help is appreciated. Thank you.
 
Have you looked up the DocumentBeforePrint Event in help and tried it? Post the code you created using it and where you put this code.


Gavin
 
Well if you read my first post completely it does say that macro 1,3 and 4 are inserting a watermark and macro 2 is deleating it!!.

Ahem. Excuse me? If that was 100% believable, and accurate...you would not be having a problem...now would you?

!!!!!

I am just to take your word that macro 1, 3, 4 are written, and working correctly? Well...OK, then make a separate Sub that fires them in the order you have...and test it. If it works, then...hey...your problem is solved!! Hurrah!

I did read your post completely.
All I want to know is how to get them to run when I print the document.
All you want to know? OK. You have already been told how to do that. There are two print commands to print. You were using one, but firing the other. But since you are having trouble with this...


Put your code in either DocumentPrint, or DocumentPrintDefault, then use the appropriate method to fire them. File > Print to fire DocumentPrint, or the Print button to fire DocumentPrintDefault.

I will repeat, since you did not seem to read my post completely.

Post your code.

Gerry
My paintings and sculpture
 
alan1203 - I sense frustration here. Note that we are not a helpdesk. Most if not all of us have day jobs but try and help anyone who comes onto Tek-Tips.

If you provide us with as much information as possible, particularly when we ask for it, you are likely to get a more complete answer.

I would like to help (hell its 11.25pm in the UK now and I am logged on here reading posts) but cannot add anymore to what has already been posted because I do not know what your macros are doing, or whether you are using the right subs.

Help us to help you.....
 
First of all I would like to apologise to Fumei (Gerry) for being short with him. I appreciate you are trying to help me and I was out of order being rude to you.
I appreciate your help.
The code that i'm trying to execute at the moment looks something like this:

Private Sub Document_Print()
Macro1
ActiveDocument.Printout, , , , , , , 1
Macro2
Macro3
ActiveDocument.Printout , , , , , , , 1
Macro2
Macro4
ActiveDocument.Printout, , , , , , , 1
Macro2
End Sub

The Code for Macro1 is:

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
ActiveDocument.Sections(1).Range.Select
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.HeaderFooter.Shapes.AddTextEffect(PowerPlusWaterMarkObject1, _
"Patient Copy", "Times New Roman", 1, False, False, 0, 0).Select
Selection.ShapeRange.Name = "PowerPlusWaterMarkObject1"
Selection.ShapeRange.TextEffect.NormalizedHeight = False
Selection.ShapeRange.Line.Visible = False
Selection.ShapeRange.Fill.Visible = True
Selection.ShapeRange.Fill.Solid
Selection.ShapeRange.Fill.ForeColor.RGB = RGB(192, 192, 192)
Selection.ShapeRange.Fill.Transparency = 0.5
Selection.ShapeRange.Rotation = 315
Selection.ShapeRange.LockAspectRatio = True
Selection.ShapeRange.Height = CentimetersToPoints(3.07)
Selection.ShapeRange.Width = CentimetersToPoints(18.41)
Selection.ShapeRange.WrapFormat.AllowOverlap = True
Selection.ShapeRange.WrapFormat.Side = wdWrapNone
Selection.ShapeRange.WrapFormat.Type = 3
Selection.ShapeRange.RelativeHorizontalPosition = _
wdRelativeVerticalPositionMargin
Selection.ShapeRange.RelativeVerticalPosition = _
wdRelativeVerticalPositionMargin
Selection.ShapeRange.Left = wdShapeCenter
Selection.ShapeRange.Top = wdShapeCenter
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

This is the same for both macros 3 & 4 except the watermark uses different text.

Macro 2 which removes the watermark looks like:

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
ActiveDocument.Sections(1).Range.Select
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.HeaderFooter.Shapes("PowerPlusWaterMarkObject1").Select
Selection.Delete
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End Sub

The macros were recoreded using the record macro feature in word and run fine when executed manually but I would like them to run when the document is sent for printing. Following the advice from Fumei and Gavona I think my code should be similar to :
Private Sub App_DocumentBeforePrint(ByVal Doc As Document, Cancel As Boolean)

Macro1
ActiveDocument.Printout , , , , , , , 1
Macro2
Macro3
ActiveDocument.Printout , , , , , , , 1
Macro2
Macro4
ActiveDocument.Printout , , , , , , , 1
Macro2

End Sub

with:
Dim X As New EventClassModule
Sub Register_Event_Handler()
Set X.App = Word.Application
End Sub

thrown in somewhere but i'm unsure where. I would be grateful if someone could explain how all this code comes together as i'm new to any programming.

Thank you
 
Ummmm. Me bad.

Try using Sub FilePrint(), or Sub FilePrintDefault().

It is not Document_Print.

Remember though that FilePrint is File > Print, and FilePrintDefault is the Print button. They are different commands.

Also, instead of all that stuff for your deleting macro, you could use to delete it directly:
Code:
Sub DeleteWatermark()
ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary) _
   .Shapes(1).Delete
End Sub

Assuming, you ARE doing this to Section 1 Primary - that is, you do not have DifferentFirstPage, or DifferentOddEven set as True.

Gerry
My paintings and sculpture
 
Works great. Exactly what i'm looking for. Thanks very much for your help and patience. Keep up your good work.
 
Shoot! Wee problem. Hope you can help. I have added protection to this document through Tools -->Protect Document. Restricted to filling in forms. When the protection is turned on i'm getting a Visual Basic Error Window "This command is not available".
When the protection is turned off the document prints no problem. Any ideas whats causing this? Thank you
 
If the document is protected you can not add the watermarks. Protecting a document locks any edits...and adding a watermark is quite decidely an edit.

BTW: rather than having three different macros to have three different text for the watermark, why not make ONE macro that uses a string as a parameter?

Code:
Sub AddWatermark(strIn As String)
' yadda yadda yadda
    Selection.HeaderFooter.Shapes.AddTextEffect(PowerPlusWaterMarkObject1, _
        strIn, "Times New Roman", 1, False, False, 0, 0).Select
' yadda yadda yadda
End Sub

In your code - for the Print button:
Code:
Sub FilePrintDefault()
ActiveDocument.Unprotect Password:=""
Call AddWatermark("Patient Copy")
   ActiveDocument.PrintOut
Call DeleteWatermark
Call AddWatermark("Yogi Bear")
   ActiveDocument.PrintOut
Call DeleteWatermark
Call AddWatermark("Whatever")
   ActiveDocument.PrintOut
Call DeleteWatermark
ActiveDocument.Protect wdAllowOnlyFormFields, Password:=""
End Sub

Gerry
My paintings and sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top