PDA

View Full Version : Solved: VBA Code to update TOC won't work



gmaxey
09-15-2005, 03:06 AM
I am having a problem with code to update a TOC.

The basic code is

Sub Update()
ActiveDocument.TablesofContent(1).Update
End Sub

Nothing happens when I execute the above code. When I step through it does'nt generate any errors.

The problem appears to be in my VB Editor. I am in deep
water here and hardly know what I am talking about. However, if I add a
second TOC and run this code:

Sub MacroTest()
Dim i As Long
For i = 1 To ActiveDocument.TablesOfContents.Count
ActiveDocument.TablesOfContents(i).Update '****
Next
End Sub

And then step though with the F8 key, I get to the the line tagged **** and the macro just terminates. For some reason the line of code is not being executed and the macro simply terminates (i.e., it does not cycle through each i).

I think a contributing factor may be that I have two Microsoft Office 11 Object Library references listed. I can uncheck one but not both (says it is in use when the VBE is open). I can shutdown Word and delete both files, but Office installer puts them both back on startup. I have removed and reinstalled Office (in desperation) but still not joy.
Very frustrating. I will be out of about 12 hours (work), but hope someone posts a solution in my absence. Thanks all.

sandam
09-15-2005, 04:38 AM
Hi, and welcome to VBAX. :wavey:

A few things. What version of Word are you running? I tested your code in 2003 and for the first procedure, all I got was a method not supported error. So, I've fiddled and come up with this that worked. I think the error comes about in that with your original line, Word is looking for more than one TOC collection.

HTH


ActiveDocument.TablesOfContents.Item(1).Update


Andrew;?

mdmackillop
09-15-2005, 05:04 AM
Hi gmaxey
Welcome to VBAX. I hope Andrew's code helps you.
When you make a post containing code, if you select the code and click on the VBA button, it formats it as above, making it easier to read.
Regards
MD

TonyJollans
09-15-2005, 05:34 AM
Hi gmaxey,

Welcome to VBAX!

There is a typo in the first statement (it should say TablesOfContents) which explains sandam's trouble with it.

There is nothing wrong with your code and it works fine for me (tested in Word 2000 and 2003) without needing to add a second TOC - a collection can have one member. It sounds like something is corrupted.

It's odd that you have two Office object libraries - what are the two locations? - but there's no obvious reason why that should cause a problem (although it may be a symptom of an underlying problem I suppose).

Uninstalling and reinstalling Office rarely achieves much! Problems are so often either in the Normal template or the registry entries for either Office or VBA, which are not cleared by uninstalling. Dreamboat has a link to somewhere on TheOfficeExperts website with a list of things to do but I don't know what it is off the top of my head.

Firstly I would confirm that the problem is not with your document (or its attached template) by trying in a new document. Next I would try deleting (or renaming) Normal.dot and then deleting (or renaming) registry keys HKCU/Software/Microsoft/VBA (VBA will recreate) or (perhaps slightly draconianly) HKCU/Software/Microsoft/Office/11.0/Word (Office Installer will recreate but may need media) (replace 11.0 with the appropriate number for your version).

If none of that resolves it, I'd be a bit stuck! Do you have Visual Studio (or VB6) installed? Sometimes I believe there can be version incompatibilities with some elements of Office.

MOS MASTER
09-15-2005, 02:49 PM
Hi Greg...Welcome to VBAX! :hi:

I'm with Tony..your code is fine but the problem lies in Office or Word corruption. (Perhaps due to multiple versions or install problems)

You talk about Microsoft Office 11 Object Library references so you must have Office 2003 installed.

The files that you deleted are those the "msWord.olb" file? (Twice)

If so are they both in the same directory?

You could try the following?

Is one of the files in "C:\Program Files\Common Files\Microsoft Shared\Office11"? (Or are they both)
If so uncheck (In VBE references) the one that is not in that folder and note the path down.
Use Unregsvr32 /u [Full path to olb file] from Start/Run to unregister that library from the registry
Delete the file
Restart Word and see if it's gone or not
Like Tony said Office leaves a lot behind in the registry and a lot of Supporting files like Normal.dot cause they are reused when you Install Office again.

A total cleanout from Office is always difficult!!

HTH, :whistle:

gmaxey
09-15-2005, 03:20 PM
My problem is solved. I just tried posting a decription that took nearly thirty minutes to type out and when I went to post this forum froze up and everything was lost.

It was a weird one but I just don't have time now to explain it all over again. Thank you all for your suggestions and interest.

MOS Master, apparently I don't have a Unregsvr32 file. I tried to run your line and it said file not found. I searched for Unregsvr32 and one wasn't found.

One of the MSWord.olb files is listed in the C:\Program Files\Microsoft Office\Office11\ directory. I did delete the file, but Office Installer just puts it right back when I restart Word. Do you know the key in the registry that I could just go to manually to delete it?

Thanks.

gmaxey
09-15-2005, 03:27 PM
Tony,



The reply below is an abbreviated reply that I posted on the Word newsgroup earlier today after I found my problem. I tried to post here after I got home and saw your posts. I spent 30 minutes on that reply and it this forum crashed when I tried to send so all was lost. Anyway you would have put me on the right course if I hadn't stumbled on it earlier. Thanks



It appears I have stumbled on a cause of the problem yet still can't
understand it. As it turns out I was having the same problem at work
using Word2000. The only thing common with my work computer and home
computer is my Normal.dot


First I am guilty of sloppy housekeeping with my normal.dot (Any
suggestions for changing my ways are welcome.) I have about 10
different modules in my Normal.dot and probably a hundred or more
macros.


I created a new Normal.dot and all worked as expected.


Next exported all of my modules from the NormalOld.Dot to the desktop.


Next I imported the modules into the new normal.dot one at a time
checking functionality of the TOC update each time. The problem
reoccurred when I imported one of the modules.


I started looking at the individual macros in that module. I found
that the following macro was causing the behavior:


'Sub UpdateFields()
'Dim oStory As Range
' For Each oStory In ActiveDocument.StoryRanges
' oStory.Fields.Update
' If oStory.StoryType <> wdMainTextStory Then
' While Not (oStory.NextStoryRange Is Nothing)
' Set oStory = oStory.NextStoryRange
' oStory.Fields.Update
' Wend
' End If
' Next oStory
'Set oStory = Nothing
'End Sub
When I stetted in out as show above the first time I ran the TOC update
routine I got and Error something like "Could not find sub or funtion).
It was like the line
ActiveDocument.TablesofContents.Update was trying to run the
UpdateFields() routine above. I close the VBE and reopened it and the
TOC routine then ran fine. I have since renamed the macro above as
myUpdateFields all is working properly at least on the Word2000 PC.

MOS MASTER
09-15-2005, 03:32 PM
My problem is solved. I just tried posting a decription that took nearly thirty minutes to type out and when I went to post this forum froze up and everything was lost.

I'm sorry for that Greg! :(

We moved to another server to increase our service to the members but we are having dificulties with the new host getting it all setup. (Without errors)



It was a weird one but I just don't have time now to explain it all over again. Thank you all for your suggestions and interest.

I hope you have the time again soon to restate the problem cause it sounds like interesting material.



MOS Master, apparently I don't have a Unregsvr32 file. I tried to run your line and it said file not found. I searched for Unregsvr32 and one wasn't found.

O ok...could you try out "Regsvr32" with the /u switch instead for info see:
http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/regsvr32.mspx



One of the MSWord.olb files is listed in the C:\Program Files\Microsoft Office\Office11\ directory. I did delete the file, but Office Installer just puts it right back when I restart Word. Do you know the key in the registry that I could just go to manually to delete it?

Thanks.

I don't know from the top of my head and I'm not running 2003 here at the moment. I'll take a quick look in the registry if I can find it otherwise I'll try to post tomorrow when I looked in my laptop (2003) for it.

HTH, :whistle:

MOS MASTER
09-15-2005, 03:37 PM
Hi Greg, :yes

Reading your last post you've encountered the most common Word problem:
Template bloat! (Document corruption)

Recreating Normal.dot is always a good bug fixer in Word (As well is rebuilding the Data key in the registry)

Corrupted templates are very hard to fix so I rather rebuild them then trying to save them.

There is a nice code cleaner on the Word mpv site that I use a lot to keep my templates clean (that is code wise)

I have all my code modules in a global template (Add-in) and that one is backed up in threefold.

Well it seams to me your problem is as good as solved so I'm glad for yah.

HTH, :whistle:

TonyJollans
09-15-2005, 03:48 PM
A-ha! Not exactly corruption. And an issue worth highlighting for others. One must be careful naming macros in Word.

UpdateFields is a Word Command and, as such, can be overridden by having a macro of the same name.

Part of the process of refreshing a TOC is to update the TOC Field so that the new values are shown in the document and this runs the UpdateFields command - which you had unwittingly overridden.

Thanks for coming back with that.

TonyJollans
09-15-2005, 03:51 PM
First I am guilty of sloppy housekeeping with my normal.dot

Me too, I'm afraid. I do know better, but, well, there's always something more interesting to do :)

MOS MASTER
09-15-2005, 03:53 PM
A-ha! Not exactly corruption. And an issue worth highlighting for others. One must be careful naming macros in Word.

UpdateFields is a Word Command and, as such, can be overridden by having a macro of the same name.


Hahaha now you have me Sir Tony!!! :bow:

I was put of by the double reference and was thinking to much about the restore of Word and Office...:*)

Very good of you to think of Word commands.

When UpdateFields is called it actualy calls:
Option Explicit
Sub UpdateFields()
'
' UpdateFields Macro
' Updates and displays the results of the selected fields
'
Selection.Fields.Update
End Sub


Time for me to go to bed! :whistle:

TonyJollans
09-15-2005, 04:03 PM
Wel te rusten!

MOS MASTER
09-15-2005, 04:04 PM
Wel te rusten!

Trusten Tony...ben nu echt weg! :yes

gmaxey
09-15-2005, 04:27 PM
Me too, I'm afraid. I do know better, but, well, there's always something more interesting to do http://vbaexpress.com/forum/images/smilies/001.gif

Exactly.

It couldn't get that regsvr32 piece to work either.

Thanks again everyone for the support and discussion.

MOS MASTER
09-16-2005, 04:44 PM
Exactly.

It couldn't get that regsvr32 piece to work either.

Thanks again everyone for the support and discussion.

Hi Greg, :yes

Well that is truly strange regsvr32 won't run for you.
I'll try that out myself this weekend and post later what I come up with...

Anyway you're problem seams solved by Tony's suggestion so I'm happy anyway. :whistle: