View Single Post
 
Old 02-21-2018, 08:11 AM
prhmusic prhmusic is offline Windows 7 64bit Office 2010 64bit
Novice
 
Join Date: Jan 2017
Posts: 6
prhmusic is on a distinguished road
Default

Well, I'm closer - thanks for your patience!

Using the code in post #11, I was able to make the macro work! It was so awesome... but only for a single folder, not its sub-folders.

So I tried to use the code from https://www.msofficeforums.com/word-...html#post47785 and have been unsuccessful.

I'm trying to learn through this exercise and noticed a line about "ScreenUpdating" and had a question about it. I saw it is set to "False":
Code:
Application.ScreenUpdating = False
If I changed that line to
Code:
Application.ScreenUpdating = True
would I see the macro doing its magic?

This is the current version of the code & I'm obviously missing something....

Code:
Dim FSO As Object, oFolder As Object, StrFolds As String
Sub Main()
Dim TopLevelFolder As String, TheFolders As Variant, aFolder As Variant, i As Long
TopLevelFolder = GetFolder
StrFolds = vbCr & TopLevelFolder
If FSO Is Nothing Then
  Set FSO = CreateObject("Scripting.FileSystemObject")
End If
'Get the sub-folder structure
Set TheFolders = FSO.GetFolder(TopLevelFolder).SubFolders
For Each aFolder In TheFolders
  RecurseWriteFolderName (aFolder)
Next
'Process the documents in each folder
For i = 1 To UBound(Split(StrFolds, vbCr))
  Call UpdateDocuments(CStr(Split(StrFolds, vbCr)(i)))
Next
End Sub
Sub RecurseWriteFolderName(aFolder)
Dim SubFolders As Variant, SubFolder As Variant
Set SubFolders = FSO.GetFolder(aFolder).SubFolders
StrFolds = StrFolds & vbCr & CStr(aFolder)
On Error Resume Next
For Each SubFolder In SubFolders
  RecurseWriteFolderName (SubFolder)
Next
End Sub
Sub UpdateDocuments(oFolder As String)
'
' updateDocuments Macro
'
'
' Sub UpdateDocuments()
Application.ScreenUpdating = False
Dim strDocNm As String, strFolder As String, strFile As String, wdDoc As Document
strInFolder = oFolder
If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "\*.docx", 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 RefreshFields(wdDoc)
      .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 RefreshFields(wdDoc As Document)
Dim oStory As Range, oTOC As TableOfContents, oTOF As TableOfFigures
With wdDoc
  For Each oStory In .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 .TablesOfContents
    oTOC.Update
  Next oTOC
  For Each oTOF In .TablesOfFigures
    oTOF.Update
  Next oTOF
End With
End Sub
Reply With Quote