Macro used to update all bookmarks, TOCs, fields, etc.

K

Karen

Hi, I can't find my original post it was a while ago where I was asking for
help on how to have a macro update my entire document (bookmarks, TOCs,
fields, headers, footers, etc.). Graham Mayor gave me a macro to use. It
worked great! I now have to create a new template which I based off of the
first one which had this macro attached to it. The macro isn't working like
it use to. When I click on the "Update All" button it doesn't update
anything. The macro that I have is posted below. Any help would be greatly
appreciated. Thanks, Karen

Sub UpdateAll()
Dim oField As Field
Dim oSection As Section
Dim oHeader As HeaderFooter
Dim oFooter As HeaderFooter
Dim oTOC As TableOfContents
Dim oFig As TableOfFigures
Dim oAut As TableOfAuthorities

For Each oTOC In ActiveDocument.TablesOfContents
oTOC.Update
Next oTOC

For Each oFig In ActiveDocument.TablesOfFigures
oFig.Update
Next oFig

For Each oAut In ActiveDocument.TablesOfAuthorities
oAut.Update
Next oAut

For Each oSection In ActiveDocument.Sections
For Each oHeader In oSection.Headers
If oHeader.Exists Then
For Each oField In oHeader.Range.Fields
oField.Update
Next oField
End If
Next oHeader
For Each oFooter In oSection.Footers
If oFooter.Exists Then
For Each oField In oFooter.Range.Fields
oField.Update
Next oField
End If
Next oFooter
Next oSection
End Sub
 
B

Bear

Karen:

I don't have any comment on your code -- it looks okay to me. Did you try
opening the macro in the VBE and stepping through it using F8? If it works
doing that, put a breakpoint at the name line (F9) of the sub and try running
it from your GUI. If you never hit that breakpoint, then your command or
button isn't linked to the macro.

Bear
 
K

Karen

Hi Bear, Thanks for the reply. I am not very good as macros or VBE but I did
open the macro in VBE and hit the F8 key. This is what happened:
* When the macro opens in VBE the cursor is at this line Dim oField As Field.
* I hit F8 and the following line highlights yellow: Sub UpdateAll()
* F8 again and the following line highlights yellow: The rest of the DIM
lines are skipped, For Each oTOC In ActiveDocument.TablesOfContents
* F8 twice and the rest of the oTOC section highlights.
*F8 again, the For Each oFig In ActiveDocument.TablesOfFigures section
highlights.
* F8 twice, the rest of the oFig section highlights.
*F8 one more time and it goes back to the oFig.Update line.
* It toggles between the oFig.Update and Next oFig lines and doesn't come
out of this.

Any suggestions as to what I should do?

Thanks, Karen
 
B

Bear

Karen:

What I was getting you to do was to run the macro one step at a time. You
should run a step or group of steps, then check the document to see the
result. I.E. to see if the macro is doing what you intended.

For example, step through the section that handles the TOC: starts with For
Each oTOC In ActiveDocument.TablesOfContents and ends with Next oTOC. Once it
goes through all these lines and the NEXT statement is highlighted, your
macro should have updated all the TOCs. Do a test, where you change the TOC
result, then run the macro, and see whether or not the TOC got reset
(updated).

When you got to the "doesn't come out of these lines" part, you may just be
experiencing a document with a LOT of oFigs in it. If the macro was really
stuck in some kind of loop, when you ran it from the GUI, it would seem to
"hang" and you'd have to Ctrl + Break to end the macro. (You didn't describe
this behavior in your original post, so I gather nothing seems to happen, but
you're not hung up.)

If you just want to see if the danged macro works at all, isolating any
problems you may have with attaching it to a button, you can open the VBE,
put the cursor anywhere in the macro, and press F5 to have the whole macro
run. You could put message box before the End Sub line, or make a breakpoint
at the End Sub line to convince yourself that it did run to the end.

MsgBox: "The macro just ran to the end"

So the point with the F5 is to make sure that when the macro runs it updates
the fields. The point with the F8 is to test each step in the macro and see
if that particular update gets done.

Finally, check out all the options in the Debug menu -- you can F8 up to a
certain point, then position the cursor further down and click Debug > Run to
Cursor (Ctrl+F8) to effectively fast-forward through long loops.

Or you can put a break point in any line further down and press F5 to
fast-forward to the break point, then resume F8 steps through the macro.

No matter which way you run the macro from the VBE, if it runs to the end
and updates all the fields in your doc, then there's nothing wrong with the
macro. It's just not getting invoked when you click your button or command in
the GUI.

Another way (sorry if this is more troubleshooting tutorial than you wanted)
to test the invocation would be to run the macro from the Tools > Macro >
Macros list. If that works, then again you're just not connecting your button
or command to the macro.

Bear
 
K

Karen

Hi Bear, I appreciate your help but I am still not finding my issue. I
changed several of my bookmarks that need to be updated through the whole
document and then put a MsgBox in before the End Sub. I get the MsgBox
everytime but the fields don't update. The TOC updates with the fields I
manually update. I have removed the button and have been updating the
document by going to Tools>macros>run after selecting my macro. I haven't had
to break the macro at all, it doesn't seem to be stuck in a loop. It seems to
be skipping the update of the fields. I can't figure out why.

When I do a step through with F8 it seems to skip the Fields all together. I
am at a complete loss. When I right click on each field and select Update
they update just fine. Is it possible my fields are messed up some how? I
think it is the macro but I just can't figure it out.

I know you have helped alot already but any additional help would be greatly
appreciated.

Thanks, Karen
 
B

Bear

Karen:

You're changing the content of bookmarked text, and you have a REF field in
a header or footer you want to update, right?

You know this macro only updates fields in the headers and footers, right?

Bear
 
K

Karen

Hi Bear, I wasn't aware that it only updates the fields in the headers and
footers. I guess that's my problem. I wonder how it worked before in the old
template that I used it for. I'm not familiar with macros really is there
anyway you could help me update this so it would update all fields (TOCs,
headers, footers, fields in main doc, etc.). Thanks, Karen
 
G

Graham Mayor

Fields can be oddly reluctant to update by macro, but one of the following
should do the trick.

Sub UpdateAll()
Dim oStory As Range
Dim oField As Field
For Each oStory In ActiveDocument.StoryRanges
For Each oField In oStory.Fields
oField.Update
Next oField
Next oStory
End Sub

Sub UpdateAllFields()
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

Sub UpdateFieldsAnotherWay()
Dim sView As String
Dim sUpdate As String
sView = ActiveDocument.ActiveWindow.View.Type
sUpdate = Options.UpdateFieldsAtPrint
Options.UpdateFieldsAtPrint = True
Application.ScreenUpdating = False
PrintPreview = True
PrintPreview = False
ActiveDocument.ActiveWindow.View.Type = sView
Options.UpdateFieldsAtPrint = sUpdate
Application.ScreenUpdating = True
End Sub
 
K

Karen

Hi, I added the additional macro to my UpdateAll module and it still isn't
working. I am going to go through the steps I did to create the macro,
module, and button to see if maybe I am missing something.

Select Tools/macro/macros
Macro Name = UpdateAll; Select Create
Selected all and pasted the following macro:

Sub UpdateAll()
Dim oField As Field
Dim oSection As Section
Dim oHeader As HeaderFooter
Dim oFooter As HeaderFooter
Dim oTOC As TableOfContents
Dim oFig As TableOfFigures
Dim oAut As TableOfAuthorities

For Each oTOC In ActiveDocument.TablesOfContents
oTOC.Update
Next oTOC

For Each oFig In ActiveDocument.TablesOfFigures
oFig.Update
Next oFig

For Each oAut In ActiveDocument.TablesOfAuthorities
oAut.Update
Next oAut

For Each oSection In ActiveDocument.Sections
For Each oHeader In oSection.Headers
If oHeader.Exists Then
For Each oField In oHeader.Range.Fields
oField.Update
Next oField
End If
Next oHeader
For Each oFooter In oSection.Footers
If oFooter.Exists Then
For Each oField In oFooter.Range.Fields
oField.Update
Next oField
End If
Next oFooter
Next oSection
End Sub

Select Save and close editor
Insert Module and named UpdateAll; pasted new macro into this module

Added the second macro UpdateAllFields to NewMacros:

Sub UpdateAllFields()
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

Added UpdateAllFields macro to the UpdateAll module.
Saved and closed VBE.

Making toolbar and button:
Select View/tollbars/customize
On the Toolbars tab selected New
Named new toolbar "SARD Tools"
On the Commands tab select macro
In the right column dragged Normal.UpdateAll.UpdateAll to new toolbar.
Renamed Normal.UpdateAll.UpdateAll to UpdateAll.

Since both macros are now in the one I didn't think I needed to add the
Normal. UpdateAll.UpdateAllFields button to the toolbar as well. Please
correct me if I am wrong on this.

When I select the UpdateAll button it updates the TOC, headers, and footers,
but still doesn't update the fields in the body of the document. I would
prefer to have one macro that does everything.

Additional help is greatly appreciated!

Thanks, Karen
 
G

Graham Mayor

The original macro you are using is specific to headers, footers, TOC, TOA
and TOF.
The macro that should do all of them and the fields in the document body is
Sub UpdateAllFields()
If that macro doesn't update all of the fields in your document, let us know
what it doesn't do.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
K

Karen

Hi Graham, The new macro UpdateAllFields is updating the fields in the body
of the doucment and the headers/footers but not the TOCs. Sorry for the
confusion I'm having. Karen =)
 
G

Graham Mayor

Are you sure it isn't updating the TOCs? It does here. Temporarily remove an
entry that should be at the start of the TOC and run the macro. Does the TOC
reflect that?
If not, let's try and force an update to the TOCs. See if the following
fares any better? ;)

Sub UpdateAllFields()
Dim oStory As Range
Dim oTOC As TableOfContents

For Each oTOC In ActiveDocument.TablesOfContents
oTOC.Update
Next oTOC

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


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>


I can understand the confusion. Update macros do not always behave as you
might imagine :)
 
K

Karen

Hi Graham, The new macro you sent works for everything. Thank you very much.
I apologize if I was a little loopy through this process. I have been out of
it for days, I guess pregnancy does that to you. Thanks again! Karen =)
 
G

Graham Mayor

I wouldn't know - I have never been pregnant ;)
Glad we got there in the end. Good luck with the new offspring, when it
arrives.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top