Document Refresh Macro

D

DeanH

I would like a macro that did the following:
Select whole document.
F9
Update Entire Table - possibliy three times.
then Repeat once.

How do you get the Update Entire table to work for different documents that
sometimes have one Table and sometimes three Tables? Also how to repeat
process only once?

I am a very basic with macros, mainly using Record, but the recorder does
not pick up the Entire Table part of the actions (only recording
Selection.Fields.Update which does not do what is required) and does not
record the possible 3x.

Also how do you repeat process once?

Many thanks in advance.
DeanH
 
G

Graham Mayor

Tables? The macro used as an example at
http://www.gmayor.com/installing_macro.htm will update all the fields. If
you need to update more thoroughly then you would have to update all the
story ranges separately. You can't do this from the macro recorder.

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


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
D

DeanH

Thanks for the response.
Tables as in a Table of Contents, and I also have two other "TOCs", one for
List of Tables and a List of Figures, these are TOCs created from the
captions.
These three require the "Update Table of Contents" dialog to go to "Update
Entire Table" three times (if the document have three such tables in it that
is, as sometimes I only have a normal TOC).
But the Macro I am wanting should also refresh any and all other fields in
the document, ie the captions themselves and cross-referencing.
You have totally lost me with the "story ranges" mentioned.
Hope this helps.
DeanH
 
G

Graham Mayor

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

should update all the fields in your document, including the tables. It
sometimes has problems with fields in header/footer, but we can address that
if it is a problem with your document.

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


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
D

DeanH

Many thanks for this, unfortunately it still does not do the major thing I
was looking for, namely Refresh the Entire Tables.
It does update the page numbers but not the Entire Table so new Headings and
Captions are captured.
Also how do you get the macro run itself once more? So when the new Headings
and Captions are captured, their new numbers are reflected in the Tables?

Thanks again.
DeanH


Fields in Header/Footer are not a problem.
 
G

Graham Mayor

It should update the entire table, however if running it twice achieves
that, you can simply loop the code to run twice eg

Sub UpdateAllFields()
Dim oStory As Range
For i = 1 To 2
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
Next i
Set oStory = Nothing
End Sub


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


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
D

DeanH

Sometimes the caption or bookmark numbering is out of sequence, say after
major shuffling, deletions and additions. Running twice ensures the tables
reflect the new order.

Unfortunately the macro still does not "Update Entire Table", only "Update
Page numbers".

For example if I copy a table caption numbered 10, then paste after - so it
will become Caption number 11. The inital number the copy has is still 10 and
also this new caption is not caputred in the List of Tables. Run the macro,
the caption is now 11, but it is still not captured in the List of Tables.
This is not to say that I copy/paste captions that much (I have a Autotext
for this) but this way clearly shows the Update Entire Table is not occuring.

Sorry to be a pain.
Thanks
DeanH
 
D

DeanH

Sorry got this the wrong way round. The macro IS updating the number for the
new caption and is capturing the new caption (with new number) on the List of
Tables but is not updating the numbering or name of any caption that was
previously on the List but had been edited or the numbering needs updating.
After running the macro, I do the right click Update Entire Table, all is
well, but that is what I am hoping the macro will do.
 
G

Graham Mayor

When all else fails - a little brute force is needed ;) See how you get on
with the following.

Sub Update()
Dim oField As Field
Dim oStory As Range
Dim oSection As Section
Dim oHeader As HeaderFooter
Dim oFooter As HeaderFooter
Dim oTOC As TableOfContents
Dim oTOF As TableOfFigures
Dim oTOA As TableOfAuthorities
For Each oTOC In ActiveDocument.TablesOfContents
oTOC.Update
Next oTOC
For Each oTOF In ActiveDocument.TablesOfFigures
oTOF.Update
Next oTOF
For Each oTOA In ActiveDocument.TablesOfAuthorities
oTOA.Update
Next oTOA
For Each oStory In ActiveDocument.StoryRanges
For Each oField In oStory.Fields
oField.Update
Next oField
Next oStory
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


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


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
D

DeanH

Many thanks Graham, especially for the perseverance ;-)

This works well. I have trimmed the code down for to fit my needs, Took out
the individual Fields updates, this stops the 250+ actions for a small 50
page document.
Took out the TOA, as I never do these. Took out the Headers and Footers as
these dont need to be updated in this process.
Moved the TOC and TOF to the end of the process so to capture the
renumbering of captions.

Here is what I have now, FYI.

Sub Update1()
Dim oStory As Range
Dim oTOC As TableOfContents
Dim oTOF As TableOfFigures
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
For Each oTOC In ActiveDocument.TablesOfContents
oTOC.Update
Next oTOC
For Each oTOF In ActiveDocument.TablesOfFigures
oTOF.Update
Next oTOF
End Sub

Thanks again
DeanH
 
G

Graham Mayor

I knew we'd get there in the end ;)

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
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