How to incorporate page counts in TOC

K

Krystal

I need to find out how to incorporate each document’s total page count into
the Table of Contents instead of the current page number.

In my case, we have 20-60 separate document files that need a single TOC
that states the total number of pages for each file.

Example:
Section No. of Pages
100 3
200 2
300 10
 
D

Doug Robbins - Word MVP

Assuming that the section 100 relates to a Section (in Word terminology) of
the document, and that you want to insert the "table of contents" in the
first Section of the document so that section 100 is actually in the second
Section of the document, you could use a macro such as the following to
construct the table

Dim i As Long, j As Long
j = 1
With ActiveDocument
For i = 2 To .Sections.Count
Selection.InsertAfter Format(i - 1, "000") & vbTab &
..Sections(i).Range.Information(wdActiveEndPageNumber) - j & vbCr
j = .Sections(i).Range.Information(wdActiveEndPageNumber)
Next i
End With

--
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, originally posted via msnews.microsoft.com
 
K

Krystal

Thanks for the input, unfortunately the section numbers do not correspond
with Word’s section numbering as they are industry specific. That being
said, is this still possible?
 
K

Krystal

Here is an example:

Section Title No. of Pages
15010 General Provisions 3
15015 Project Closeout 10
15020 Special Conditions 5


(Each of these "sections" are saved in individual .doc files, no section
breaks used)

I thinking of using a field reference for the section number and title in
the beginning of each document to populate into to the TOC or use the
selected style in the TOC options although this doesn't help the total page
count situation.


Thanks!
 
D

Doug Robbins - Word MVP

If all of the files are in a folder and named with Section and Title, the
following macro should do what you want. Note however that the number of
pages may depend up the active printer.

Dim SourcePath As String
Dim SourceFile As String
Dim Target As Document
Dim Source As Document
Dim numpages As Long
Set Target = ActiveDocument
'Modify the following line so that it points to the folder containing the
files
SourcePath = "C:\Test\"
SourceFile = Dir$(SourcePath & "*.doc")
Do While SourceFile <> ""
Set Source = Documents.Open(SourcePath & SourceFile)
numpages = Source.BuiltInDocumentProperties(wdPropertyPages)
Target.Range.InsertAfter MyName & vbTab & numpages & vbCr
MyName = Dir
Loop


--
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, originally posted via msnews.microsoft.com
 
K

Krystal

Doug,
Thank you for the macro code, unfortunately I cannot get it to work, and I’m
not sure how to actually debug the macro. I loaded the code into Visual
Basic, named it, and created a button for it. I inputted the source path
into the requested quotations, but nothing happens. No error, dialog box,
nothing.

Can I add a prompt (dialog box) asking for the sourcepath instead of having
to place it directly into the marco? My VBA Replace macro allows that, but I
cannot access the code to copy into this one.
Thank you, Krystal
 
K

Krystal

I am getting a Runtime error '5825': Object has been deleted. When in debug
mode, it highlights the line toward the bottom that starts with
"Target.Range."

The file dialog box part worked, thanks for adding that.

Thanks again,
Krystal
 
K

Krystal

Good news, I was able to get the macro to work with some minor adjustments.
Do you have any suggestions on excluding the ".doc" text from the file name?
 

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