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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Counting Pages through VBA Code - Need Help

Status
Not open for further replies.

Kamstars

Programmer
Dec 3, 2003
6
US
I am trying to count pages in a word document after performing a merge. The name of the document, page numbers, and date are supposed to print out to a text file. The problem is that each time I am only coming up with 1 page. Are you able to tell me what I am doing wrong below? How can I get the new merged document to print out the correct number of pages?

Thanks for your help.


Sub mcrPerformMerge()

Dim FlnNamWDate As String
Dim Date1 As Date
Dim Month1 As String
Dim Day1 As String
Dim Year1 As String
Dim Date2 As String
Dim varNumberPages As Variant

Close #1

Open "h:\LetterPageInfo.txt" For Append As #1

With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.Execute
End With


Date1 = Date
Month1 = DatePart("m", Date1)
Day1 = Right("0" & DatePart("d", Date1), 2)
Year1 = Right(DatePart("yyyy", Date1), 2)
Date2 = Month1 & Day1 & Year1

FlnNamWDate = "H:\EnrollmentClosedLetter " & Date2 & ".doc"

ActiveDocument.SaveAs FileName:=FlnNamWDate, FileFormat:=wdFormatDocument, LockComments:=False, Password:="", _
AddToRecentFiles:=True, WritePassword:="", ReadOnlyRecommended:=False, _
EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData _
:=False, SaveAsAOCELetter:=False

varNumberPages = ActiveDocument.Content.Information(wdActiveEndAdjustedPageNumber)

Print #1, "EnrollmentClosedLetter ", varNumberPages, Date$


ActiveDocument.Close
Application.Quit SaveChanges:=wdDoNoSaveChanges

Close #1

End Sub

 
Hi,
Code:
varNumberPages = ActiveDocument.StoryRanges(ActiveDocument.StoryRanges.Count).Information(wdActiveEndAdjustedPageNumber)
:)


Skip,

Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Hi Kamstars,

After a Mail Merge, you have multiple copies of the same thing with different merged values. The PageNumber is repeated for each copy and does not reflect the total number of pages in the document. You should, instead, use ..

Code:
[blue]varNumberPages = ActiveDocument.Content.Information([highlight]wdNumberOfPagesInDocument[/highlight])[/blue]

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
 
Thank you for your response. I added in your additional code and am getting the following error:

"The requested member of the collection does not exist."

Any thoughts?

Thanks
 
Thank you so much Tony. That worked perfectly.

Thanks again.

 
Hi Kamstars,

Can I just add an afterthought to this? What you had wrong was using wdActiveEndAdjustedPageNumber, which is the same on each copy created by the Merge.

Although it won't be used by Word, the (unadjusted) Page Number - Information(wdActiveEndPageNumber) - at the end of the Content will give the same result as the NumberOfPages.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top