Link to home
Start Free TrialLog in
Avatar of srauler58
srauler58Flag for United States of America

asked on

Generating a Word Table of Contents without Styles

In my job I use an Oracle-based system to generate work estimates, with each estimate saved as a record.  When I need to print the entire collection out, the tool generates a Word document instead of handling printing duties itself.  The Word document generated is over 400 pages long with more than 200 individual task estimate forms, some as long as 10 pages, with the task forms separated by a section break.  There are no page numbers in the raw output from the database.  

Wish List:
"  Would like to decrease the manual effort, even automate the page numbering process as much as possible.  Selecting the entire document and trying to insert page numbers doesnt appear to work as the command only affects the current section.
"  Would like to minimize the manual labor required to create a Table of Contents using text found in a specific table cell on each task form (see example on next page).

I'm using Word 2007.  The document has to be produced on demand, usually every couple of weeks.  It currently takes me hours to edit the document by hand, adding page numbers section by section and then selecting the specific words I want in the Table of Contents and using the TOC Add Text button to add them to the TOC.  I'd like to create a macro to do as much of the work as possible, but I'm more familiar with Excel's object model than Word's, so I'm starting at square one.  Barring a macro approach, I need some tips to better understand some of Word's capabilities and limitations when it comes to generating a TOC and page numbering.

A sample document is attached that contains two edited task estimate forms and a hand worked TOC stub.  I noticed that I can't copy and paste text from the forms also... must be some kind of protection that I haven't figured out how to manage in Word 2007 yet.

Any help or a pointer to the required resources would be greatly appreciated.


Sample-Word-Doc.doc
Avatar of pedro_sland
pedro_sland
Flag of United Kingdom of Great Britain and Northern Ireland image

I'm assuming that the page numbers are supposed to restart for each report, otherwise it looks great to me. If so, then switch to the footer, select the first page of that section, header and footer tools, page number, format page numbers, start at 1.

It appears that the table cell you selected has a style of "BOE Title". If this was put there by your database then on the first page, references, table of contents, insert table of contents ..., options, remove the 1 in the heading 1 field and add a 1 in the BOE Title field and remove other numbers. Tick show page numbers, right align and set the tab leader to dots.
Avatar of srauler58

ASKER

Thanks for the quick reply, Pedro, and the opportunity to add some clarification.

Actually, the numbering is continuous, 1 thru 417 at the moment.  The problem is that there are upwards of 200 section breaks in the document, so it's labor intensive to go thru them one at a time, changing each footer to "Link to Previous" to make the page numbering continuous while leaving the headers alone so they display the ID for each record.  I did that today, but I hope to come up with a better approach.  I'm also petitioning the developer to enhance the export routine, but I think I'm going to have to come up with a local solution for the immediate future.

Unfortunately the database didn't create the "BOE Title" style, I did.  I was toying with the idea of going through all 200 sections, changing the style of that field to the custom "BOE Title" style and then changing the TOC options as you suggest.  I may still have to do it that way, or forget the idea of adding a TOC.

I'm not sure there's any easy way to do what I'm wanting to do, but I figured if there is, someone on this site will know it!  Hopefully this additional info will help...
ASKER CERTIFIED SOLUTION
Avatar of Eric Fletcher
Eric Fletcher
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Eric:  Thanks for the assist!  Your tips worked great, and I learned quite a bit in the process.  I'm awarding you the points for your solution.  Do you have any suggestions for making the process of setting the Task Title to the "BOE Title" style a bit easier.  I'm doing a Find on "Task Title" and tabbing once to get to the text I want in the TOC, then clicking on the "BOE Title" style in the floating toolbar, but there are 168 of them across 418 pages and it's time consuming.  Any tips on how to speed up this process?

Thanks again!

Steve R.
Sure... record a macro and then edit it a bit to make it loop. The steps are pretty easy in your case: use Find to get to the next "Task Title:", press tab to select the content in the next cell, apply the style -- and repeat until Not Found.

Note that you can repeat pretty quickly manually as well: do it once, then use Shift-F4 to repeat the Find, tab, and F4 to repeat the style...
Eric:  Thanks for the tips.  The manual method is handy, but I went ahead and put a looping macro together that seems to work pretty well.  Here's the code, cobbled together from the macro recorder and snippets from the web:

Sub FindTaskTitle()
   Dim FindRange As Range
   Set FindRange = ActiveDocument.Range
   With FindRange.Find
       .ClearFormatting
       .Forward = True
       .Format = False
       .Wrap = wdFindStop
       .MatchWildcards = False
       .Text = "Task Title:"
     Do While .Execute
         With Selection.Find
           .Text = "Task Title:"
           .Replacement.Text = ""
           .Forward = True
           .Execute
         End With
         Selection.MoveRight Unit:=wdCell
         Selection.Style = ActiveDocument.Styles("BOE Title")
     Loop
   End With
End Sub
Excellent; that should do it for you -- and save you a lot of time!
Eric: I ran into some issues using my macros today, and I think my dearth of knowledge regarding Word 2007's styles may be the actual cause of the issue.  Today when I ran the script above on another export file, every time Task Title was changed to the BOE Title style another row was inserted below, as if another tab had been entered.  The new row was also set to BOE Title style.  The script stalled about half way through the file and I'm not sure why that happened.

I believe the issue may be related to how I'm setting up the BOE Title style.  Is this supposed to be a character only style?  If so, I'm having a hard time figuring out how to make that happen in Word 2007. I've set it up as a paragraph style and a linked paragraph and character style, with the same unacceptable results.  Could a paragraph or linked style cause the generation of another row? Any pointers would be greatly appreciated, even pointers to a site with in-depth tutorials on Word 2007 styles.

- Steve R.
Determined that the issue is with my loop code... execution doesn't exit the loop at the right point.  Any tips on the loop?  Seems like I'm close, but it's late and I'm going to bed for now.  Thanks!

- Steve R.