Solved

Generating a Word Table of Contents without Styles

Posted on 2009-05-08
9
651 Views
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.


Sample-Word-Doc.doc
0
Comment
Question by:srauler58
  • 5
  • 3
9 Comments
 
LVL 2

Expert Comment

by:pedro_sland
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.
0
 

Author Comment

by:srauler58
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...
0
 
LVL 21

Accepted Solution

by:
EricFletcher earned 500 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.
0
 

Author Comment

by:srauler58
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.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 21

Expert Comment

by:EricFletcher
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...
0
 

Author Comment

by:srauler58
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
       .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
0
 
LVL 21

Expert Comment

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

Author Comment

by:srauler58
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.
0
 

Author Comment

by:srauler58
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.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

If you work with Word a lot, you probably use styles. If you use styles a lot, you've probably balled your fist more often than not when working with the ribbon. In Word 2007/2010, one of the things that I find missing when using styles is a quic…
Nice table. Huge mess. Maybe this was something you created way back before you figured out tabs or a document you received from someone else. Either way, using the spacebar to separate the columns resulted in a mess. Trying to convert text to t…
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now