Linker IT Software
Google
Web www.oraxcel.com
menubar-top-links menubar-top-rechts
Home Help Search Login
Welcome, Guest. Please Login.
SQL*XL: Database to Excel bridge litLIB: Excel power functions pack ExcelLock: Locking and securing your valuable Excel spreadsheets encOffice: Protect your Excel file easy and safe encOffice: Protect your Excel file easy and safe
Pages: 1
Update all fields in a MS Word document (Read 1637 times)
Gerrit-Jan Linker
YaBB Administrator
*****




Posts: 75
Update all fields in a MS Word document
14.04.15 at 09:06:55
 
Update all fields in a MS Word document
 
In a large Word document I could not find a way to get all fields to update. The usual trick Ctrl-A (select all text) and F9 (calculate) wouldn't work anymore.
 
I figured that a quick call of the following code works fine. It needs to be run from the macro environment (Alt-F11) in the immediate window (Ctlr-G). Simply type the following command and hit enter:
ActiveDocument.Fields.Update
 
If that still doesn't work, the following small macro will do it the hard way. This macro loops all the fields in your document and updates them all one by one. It will figure out which update has change the value of the field and it will print a list in the debug pane of all fields that changed.  
 
Public Sub UpdateMyFields()
    Dim fld As Field
    For Each fld In ActiveDocument.Fields
         Doevents
       str1 = fld.Result
       fld.Update
       str2 = fld.Result
       If str1 <> str2 Then
           Debug.Print str1, str2
       End If
    Next fld
End Sub
Back to top
 
« Last Edit: 07.03.16 at 10:24:55 by Gerrit-Jan Linker »  

Gerrit-Jan Linker
Linker IT Software
Email WWW Gerrit-Jan Linker   IP Logged
Gerrit-Jan Linker
YaBB Administrator
*****




Posts: 75
Re: Update all fields in a MS Word document
Reply #1 - 12.02.16 at 10:08:08
 
To show all field results instead of field codes, the following code can be used.
 
Public Sub hf()
    Dim fld As Field
    'Update fields in all headers and footers
    'First loop thru all sections in the document
    For Each ThisSection In ActiveDocument.Sections
       'Loop thru all Headers in section
       For Each ThisHeaderFooter In ThisSection.Headers
           'Pick the section and update
           
           For Each fld In ThisHeaderFooter.Range.Fields
       If fld.ShowCodes = True Then
           fld.ShowCodes = False
       End If
           Next fld
       Next ThisHeaderFooter
       'Loop thru all Footers in each Section
       For Each ThisHeaderFooter In ThisSection.Footers
           'Pick the section and update
           
           For Each fld In ThisHeaderFooter.Range.Fields
       If fld.ShowCodes = True Then
           fld.ShowCodes = False
       End If
           Next fld
       Next ThisHeaderFooter
    Next ThisSection
 
End Sub
 
 
Public Sub abc()
    Dim fld As Field
     
    Selection.WholeStory
    For Each fld In Selection.Fields
       If fld.ShowCodes = True Then
           fld.ShowCodes = False
       End If
    Next fld
     
End Sub
Back to top
 
 

Gerrit-Jan Linker
Linker IT Software
Email WWW Gerrit-Jan Linker   IP Logged
Pages: 1