Generating a Word Table of Contents without Styles

Posted on 2009-05-08
Medium Priority
Last Modified: 2012-06-27
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.

Question by:srauler58
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3

Expert Comment

ID: 24340475
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.

Author Comment

ID: 24340954
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...
LVL 21

Accepted Solution

Eric Fletcher earned 2000 total points
ID: 24341463
The TOC can be generated from your "BOE Title" styles with a field code like this: { TOC \t "BOE Title,1" }

Do you need the section breaks? If not, use Find and Replace to remove them so you only have a single section. To change section breaks to page breaks, use ^b in the find what and ^m in the replace with box.

To get your ID# in the header, consider using the STYLEREF field code. You will need to set the content in a style for this to work, but you can do that easily with Find and Replace:

a) Find what: (ID: )([0-9]{1,3})
b) Replace with: \1\2
    and then use the Format Style button and choose a character style; I used the "Emphasis" built-in style
c) Turn on the Use wildcard checkbox

This will find every instance of your ID strings (actually, any ID: space followed by a 1-3 digit number) and then set the style to Emphasis (the \1\2 represents the two found expressions, and the format sets the found content in the named style). You will probably want to modify the Emphasis style definition to match what you need. If the ID string is used elsewhere, you may need to modify the find expression.

Now you have a uniquely styled element that can be pulled into the header with the STYLEREF field. In your header -- and this way you only need a single one for th whole book -- add the field code where you need the ID reference. It will look something like this: { STYLEREF  Emphasis } assuming you used the Emphasis style. It pulls the current value of content tagged with the named style from the page.

Of course, if you can generate the style tag from the Oracle report, you won't need the interim step to set the ID as a character style. Note that I used a character style and not a paragraph style: the way your sample is set up, a paragraph style would bring in the full content of the line you have in the top cell.

If you are unable to delete the section breaks -- and it may not be possible if they are within a table -- you can remove the header and footers with some VBA code. See GrahamSkan's elegant bit of code at http://www.experts-exchange.com/Programming/Languages/Visual_Basic/Q_24371513.html for an idea about how to do so. Note that you could modify this code to set up whatever header/footers you need.
On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.


Author Comment

ID: 24342536
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.
LVL 21

Expert Comment

by:Eric Fletcher
ID: 24343875
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...

Author Comment

ID: 24344578
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
       .Forward = True
       .Format = False
       .Wrap = wdFindStop
       .MatchWildcards = False
       .Text = "Task Title:"
     Do While .Execute
         With Selection.Find
           .Text = "Task Title:"
           .Replacement.Text = ""
           .Forward = True
         End With
         Selection.MoveRight Unit:=wdCell
         Selection.Style = ActiveDocument.Styles("BOE Title")
   End With
End Sub
LVL 21

Expert Comment

by:Eric Fletcher
ID: 24344932
Excellent; that should do it for you -- and save you a lot of time!

Author Comment

ID: 24381604
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.

Author Comment

ID: 24381904
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.

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm writing to share my clumsy experience in using this elegant tool so you can avoid every stupid mistake I made. (I leave it to the authorities to decide if this deserves a place in the Knowledge archives.)  Now that I am on the other side of my l…
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
Suggested Courses
Course of the Month14 days, 9 hours left to enroll

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question