Macro to run at time of print...

C

Chr1551

I'm trying to get a single word document to print out a number of pages with
a sequentially numbered field using a bookmark and implementing the below
macro code that I found at this link:
http://word.mvps.org/FAQs/MacrosVBA/NumberCopiesOf1Doc.htm

Dim Message As String, Title As String, Default As String, NumCopies As Long
Dim Rng1 As Range

' Set prompt.
Message = "Enter the number of copies that you want to print"
' Set title.
Title = "Print"
' Set default.
Default = "1"

' Display message, title, and default value.
NumCopies = Val(InputBox(Message, Title, Default))
SerialNumber = System.PrivateProfileString("C:\Settings.Txt", _
"MacroSettings", "SerialNumber")

If SerialNumber = "" Then
SerialNumber = 1
End If

Set Rng1 = ActiveDocument.Bookmarks("SerialNumber").Range
Counter = 0

While Counter < NumCopies
Rng1.Delete
Rng1.Text = SerialNumber
ActiveDocument.PrintOut
SerialNumber = SerialNumber + 1
Counter = Counter + 1
Wend

'Save the next number back to the Settings.txt file ready for the next use.
System.PrivateProfileString("C:\Settings.txt", "MacroSettings", _
"SerialNumber") = SerialNumber

'Recreate the bookmark ready for the next use.
With ActiveDocument.Bookmarks
.Add Name:="SerialNumber", Range:=Rng1
End With

ActiveDocument.Save

My problem is that when I add the code to either the FilePrint Command or by
the method below, word freezes.

Private Sub oApp_DocumentBeforePrint(ByVal Doc As Document, _
Cancel As Boolean)
'Your code here
End Sub

Am I going about this in the correct manner? Should I be creating a word
template or document for the macro to run in? Im quite confused and any help
would be great! Thanks.
 
C

Chr1551

OK after a bit of fiddling i've got some output :) problem is that it printed
no's 1 & 2 but won't go past 2 - im getting a runtime error and directed at
this line : Set Rng1 = ActiveDocument.Bookmarks("SerialNumber").Range
Also when I closed and reopened my document it told me that macros are
disabled!! how do i reenable them?!
Thanks again!
 
C

Chr1551

Further update - the error i'm getting is 'the requested member of the
collection does not exist' i've also noticed that when the document has
printed once, the bookmark disappears - could this cause the error? I'm
running it of a normal document, would a template help? thanks.
 
D

Doug Robbins

I just ran the code from the website and also the code from your post and it
works fine for me.

The fact that the bookmark is deleted after the first document is printed is
of no consequence as it is the Rnge1 that is being used as the location for
the insertion of the numbers.

I think the problem is your trying to invoke the macro by use of the
FilePrint command. What is probably happening is that the macro is being
fired again by the ActiveDocument.PrintOut command and at that point there
would not be a bookmark to set the Rng1 to.

I would not recommend that you invoke the macro in this way as if you did,
it would run for all documents that you print.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
C

Chr1551

Thanks very much for getting back to me - would you be able to recommend an
alternative way to invoke the macro? Being a beginner and having spent the
night searching for a solution - I still haven't been able to get this to
work in the correct manner! Thanks again.
 
D

Doug Robbins

Just give it some other name and invoke it via the Tools>Macro menu item.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top