Update all docvariables in all active documents

R

Rob M

Hi,

Can anyone tell me how to use VBA to update all docvariable fields in
all active documents? I know how to update a single document, but I'm
having trouble cycling through all active documents.

Thank you,
Rob
 
G

Greg Maxey

Probably a bit of overkill as this would update "all" fields (not just
docVariables fields).

Sub ScratchMaco()
Dim oDoc As Word.Document
For Each oDoc In Documents
UpdateFields oDoc
Next oDoc
End Sub
Sub UpdateFields(Doc As Word.Document)
Dim pRange As Word.Range
Dim oShp As Shape
Dim iLink As Long
Dim TOC As TableOfContents
Dim TOF As TableOfFigures
Dim TOA As TableOfAuthorities
Dim pAlerts As String
pAlerts = Application.DisplayAlerts
Application.DisplayAlerts = wdAlertsNone
iLink = Doc.Sections(1).Headers(1).Range.StoryType
For Each pRange In Doc.StoryRanges
Do
pRange.Fields.Update
Select Case pRange.StoryType
Case 6, 7, 8, 9, 10, 11
If pRange.ShapeRange.Count > 0 Then
For Each oShp In pRange.ShapeRange
If oShp.TextFrame.HasText Then
oShp.TextFrame.TextRange.Fields.Update
End If
Next oShp
End If
End Select
Set pRange = pRange.NextStoryRange
Loop Until pRange Is Nothing
Next
Application.DisplayAlerts = pAlerts
For Each TOC In Doc.TablesOfContents
TOC.Update
Next TOC
For Each TOA In Doc.TablesOfAuthorities
TOA.Update
Next TOA
For Each TOF In Doc.TablesOfFigures
TOF.Update
Next TOF
End Sub
 
D

Doug Robbins - Word MVP

A somewhat shorter macro

If Options.UpdateFieldsAtPrint = False Then
Options.UpdateFieldsAtPrint = True
With ActiveDocument
.PrintPreview
.ClosePrintPreview
End With
Options.UpdateFieldsAtPrint = False
Else
With ActiveDocument
.PrintPreview
.ClosePrintPreview
End With
End If


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 

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