View Single Post
 
Old 12-14-2022, 05:18 PM
learning2automate1 learning2automate1 is offline Windows 10 Office 2021
Novice
 
Join Date: Dec 2022
Posts: 1
learning2automate1 is on a distinguished road
Question Batch update Find and Replace in Header and Footers on multiple Word files

Hi, I've been searching for the past few months on how to automate some of my mudane tasks at work. We typically update headers and footers or headers/footers separately to change dates or issuance names. I was able to find a macro to batch update headers which was awesome. My next task is to be able to find and replace a few words in the header and footer in multiple doc and docx files. I've tried the code mentioned however nothing happens and I believe it's because find/replace is not searching header/footer. I have another macro that works when i run it by itself however when adding it to the marco above I'm getting errors. Can you please tell me what's wrong with the macro and or how I can simplify it?



Code:
Application.ScreenUpdating = False
Dim strInFolder As String, strOutFold As String, strFile As String, wdDoc As Document
strInFolder = GetFolder
If strInFolder = "" Then Exit Sub
strFile = Dir(strInFolder & "\*.doc", vbNormal)
'Check for documents in the folder - exit if none found
If strFile <> "" Then strOutFold = strInFolder & "\Output"
'Test for an existing outpfolder & create one if it doesn't already exist
If Dir(strOutFold, vbDirectory) = "" Then MkDir strOutFold
strFile = Dir(strInFolder & "\*.doc", vbNormal)
While strFile <> ""
  Set wdDoc = Documents.Open(FileName:=strInFolder & "" & strFile, AddToRecentFiles:=False, ReadOnly:=True, Visible:=False)
  With wdDoc
Dim rngStory As Range

  For Each rngStory In ActiveDocument.StoryRanges

    With rngStory.Find

      .Text = "ICU Pavilion"

      .Replacement.Text = "ICU Pavilion Increment 5"

      .Wrap = wdFindContinue

      .Execute Replace:=wdReplaceAll
    With rngStory.Find
    
      .Text = "DR 26"

      .Replacement.Text = "DR 44"

      .Wrap = wdFindContinue

      .Execute Replace:=wdReplaceAll
   With rngStory.Find

      .Text = "10/03/2022"

      .Replacement.Text = "01/10/2023"

      .Wrap = wdFindContinue

      .Execute Replace:=wdReplaceAll
 End With
    'Save and close the document
    .SaveAs FileName:=strOutFold & .Name, AddToRecentFiles:=False
    .Close
  End With
  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



I'm receiving "compile error: function call on left hand side of assignment must return variant object" this on GetFolder
Reply With Quote