Sequential numbering including total pages

B

Bruce

I don't know if I should be asking this here or in
numbering. I'll start here. I have a one page document
(for a log book) that needs to be sequentially numbered.
I have applied the code at
http://word.mvps.org/FAQs/MacrosVBA/NumberCopiesOf1Doc.htm,
which works as intended. However, I would also like to
include the total number of pages (Page X of Y). For easy
reference, here is the VBA code (SerialNumber is the name
of a bookmark in the document footer):

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

' Set prompt.
Message = "Enter 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

How can I add the total number of pages based on the entry
in the message box? I can use something like the
following (instead of Rng1.Text = SerialNumber):
Rng1.Text = "Page " & SerialNumber & " of 100", which
works fine as long as I need 100 copies. Is there a way
that the number I typed when the macro started (when the
message box came up) can replace " of 100" in the
expression above?
I am not saving the document, just printing it, so I have
removed the last line of code. Also, in the code
following the remark 'Save the next number back to the
Settings.txt ... I used = 1 rather than = SerialNumber in
order to reset the text file to start over from page 1
each time (several templates will be using this same
code). Is that a good approach to those issues?
I realize I could manipulate this easily enough either by
changing the code or by placing the bookmark SerialNumber
thus: Page (SerialNumber) of 100 in the footer or
something like that, but I am trying to have this system
be self-perpetuating so that it does not require my direct
intervention each time.
 
D

Doug Robbins

Use Rng1.Text = "Page " & SerialNumber & " of " & Numcopies

--
Please respond to the Newsgroup for the benefit of others who may be
interested. Questions sent directly to me will only be answered on a paid
consulting basis.

Hope this helps,
Doug Robbins - Word MVP
 
B

Bruce

Thanks, that worked perfectly. I thought I had tried
something like that, but apparently not.
Another thing I would like to do is to add another piece
of information (call it Machine Number) to the document
before I print it. I believe I would create a bookmark
(MachineNum) and refer to it in the VBA code, but the
details escape me.
The code I provided is in a macro called AutoNew. I don't
know if it should be there, or in AutoOpen or
Document_Open or what. I want it to run when I start a
new document based on the template. In practice that will
be each time the user double clicks on the .dot file. I
need the prompt for Machine Number to occur before the
prompt for the number of pages, since once that prompt has
been answered the printing starts.
 
B

Bruce

I will be away for several days, so if somebody responds
and I don't acknowledge it right away, please don't think
me rude. I am still hoping to hear a solution. Thanks.
 
D

Doug Robbins

Just use another input box to ask for the Machine Number. AutoNew is what
you should be using.

At some point, if you want to ask the user for additional pieces of
information, it would be better to go to a UserForm than have a series of
Input Boxes.

See the article "How to create a Userform" at:

http://word.mvps.org/FAQs/Userforms/CreateAUserForm.htm


--
Please respond to the Newsgroup for the benefit of others who may be
interested. Questions sent directly to me will only be answered on a paid
consulting basis.

Hope this helps,
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