Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #17  
Old 12-14-2022, 05:18 PM
learning2automate1 learning2automate1 is offline Run a macro on multiple docx. files Windows 10 Run a macro on multiple docx. files 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
 

Tags
multiple files



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to open Docx files? mond_bees Word 12 08-29-2012 03:32 AM
Run a macro on multiple docx. files convert multiple csv files to multiple excel files mit Excel 1 06-14-2011 10:15 AM
Run a macro on multiple docx. files looking for macro for multiple files bolk Word 3 05-03-2011 05:46 AM
Run a macro on multiple docx. files macro to pull data from multiple files psrs0810 Excel 2 10-25-2010 01:49 PM
Icon for docx files Jazz43 Word 2 10-20-2009 08:34 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:45 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft