View Single Post
 
Old 02-19-2018, 01:12 PM
prhmusic prhmusic is offline Windows 7 64bit Office 2010 64bit
Novice
 
Join Date: Jan 2017
Posts: 6
prhmusic is on a distinguished road
Question Update fields in all documents in a folder

I'm trying to apply the code in https://www.msofficeforums.com/word-...ts-folder.html but substituting a macro I have called RefreshFields2, but it doesn't seem to be working. What am I missing?
Code:
Sub UpdateDocuments()
'
' updateDocuments Macro
'
'
' Sub UpdateDocuments()
Application.ScreenUpdating = False
Dim strDocNm As String, strFolder As String, strFile As String, wdDoc As Document
strFolder = GetFolder
If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "\*.doc", vbNormal)
strDocNm = ThisDocument.FullName
While strFile <> ""
  If strFolder & "\" & strFile <> strDocNm Then
    Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
    With wdDoc
      Call refreshFields2
      .Close SaveChanges:=True
    End With
  End If
  strFile = Dir()
Wend
Set wdDoc = Nothing
Application.ScreenUpdating = True
End Sub
Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function
Sub refreshFields2()
' Macro to update whole document, Fields and TOCs
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
    ActiveDocument.Save
    .Close SaveChanges:=True
End Sub

Last edited by macropod; 02-21-2018 at 03:57 PM. Reason: Split from: http://www.msofficeforums.com/word-vba/37236-loop-through-all-documents-folder.html
Reply With Quote