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

Update Word Cross-References Automatically 2

Status
Not open for further replies.

wokkie

Programmer
May 9, 2003
52
GB
Hi,

I am using Word 2003. I have Cross-References to other parts of the doc. I can update them manually, by right-clicking, update, but I would like them to update automatically as I make changes to the doc.

How do I do this?

Many thanks

Ian
 
Code:
Dim mField As Field
For Each mField In ActiveDocument.Fields
    mField.Update
Next

Note updating field codes in headers/footers (or other "stories") takes a bit more fussing.



Gerry
 
I have used the code above and it does exactly what I need but it nearly caught me out, so I figured I'd post even though the thread's been dead for a while.

I created three tables in my document and gave each a caption, 'Figure 1' to 'Figure 3'. In my text I made cross references to 'Figures 1' and '3' but not to 'Figure 2'. Crucially, my cross references to 'Figure 3' preceded the actual table and its caption.

I then deleted just the caption 'Figure 2' from my second table, and nothing changed. In other words, the caption 'Figure 3' stayed as 'Figure 3' and was not changed to 'Figure 2'.

I expected to be able to run the code and have it change any references to 'Figure 3' from 'Figure 3' to 'Figure 2', but this didn't happen. In retrospect, it is obvious because the code works sequentially through the document checking both cross references and captions. When it gets to the references that precede 'Figure 3', it checks that they are valid and of course they are at that point. It is only later, when the code gets to the third table in the document that it changes caption 'Figure 3' to 'Figure 2'.

Thus you end up with a document that contains references to a caption 'Figure 3' that no longer exists.

The simple solution is to run the code twice...
 
Excellent point, and thoughtful ofyou to repost.

This is also applicable to other types of code that reference field objects in Word. For example formfields (if you do not explicitly name them) can be renamed when one is deleted (or renamed and the other not). Word, in some cases, does updates by the order they appear in the document, and in other cases by the order they were entered into the document (regardless of their current order). An example if the latter are InLineShape objects - AxtiveX controls among others.

One should always try and fuss with things - error trapping is the biggest load of all.

Thanks for the post.

Gerry
 
Using:
ActiveDocument.Fields.Update
will update all fields in the body of the document simultaneously, rather than looping through them. This will avoid out-of-order updating issues.

Cheers
 
This is EXACTLY my problem, too, but I don't understand how to use the code posted here. Is it too difficult to explain?
 
No. You can put macropod's code in any applicable code module. For example, you can put the following into the ThisDocument code module.

Sub AllFields()
ActiveDocument.Fields.Update
End Sub

Now you have a macro that updates all fields.

To access the macro (the long way):

Tools > Macro > Macros
ALL documents have a ThisDocument code module. If the ThisDocument you used was in Normal.dot, then you should see a listing for AllFields. Click the Run button. If it was in another document, you will have to change the file in the Macros in; drop down.

Alternatively, you can bring the macro (or any macro) and put it in as a menu item:

Right click the toolbar you want to put the macro as a menu item. Select Customize. Select the Commands tab. Select macros. Scroll through the right column (which lists macros) and find your macro. Drag it to the menu item. You can also drag it out to sub menus. Release. To rename it (and it will have a ridiculously long name), right click the (new) menu item, select Name: and type whatever you want. You can also put an icon. When you are done, select the Close button on the Customize dialog.

You can also drag a macro directly on to a toolbar (after rightclick Customize). This creates a macro icon/button right on the toolbar. Again you can rename it, or replace it with an icon.

Now whenever you want to run that macro, just click the icon.

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top