#  Other Applications & Softwares  > Word Formatting & General >  >  Macro to Batch/Mass Edit Headers/Footers on Multiple Word Documents (.docx) - Replace Text

## JamesFrames8

I need to edit the Headers on 37 documents and change the same phrase of text. I need a macro that finds a standard text phrase among the 37 word documents and replace it with a specific text phrase.

I located a decent Macro that will edit the _Body_ of the text but it won't change the header or footers (link below)

http://www.extendoffice.com/document...ple-files.html

----------


## macropod

Try:



```
Please Login or Register  to view this content.
```


Note: the macro has its own folder browser and processes all files in the selected folder. As coded, it processes every possible range in a document, but the comments indicate what can be deleted to restrict processing to, say, just the headers.

----------


## JamesFrames8

Which points in the Macro do I need to add my inputs - which section do I put what I want to find and which section do I put what I need to replace?

----------


## macropod

The inputs are in the line:
Fnd = "Find Text": Rep = "Replace Text"
Change what's between the double-quotes for whatever you require.

----------


## gonecrawfishin

I'm getting a ByRef argument type mismatch error when I use this code.  Thoughts?

----------


## macropod

How are you running the code and what code line, if any, is highlighted when the error occurs (after clicking 'debug' if you're given that option)?

----------


## gonecrawfishin

Hi,

The Rng in           Call RngFndRep(Rng, Fnd, Rep) is highlighted.  What do you mean by "how do you run the code"?  This happens if I run it from the developer window, or if I run it via "compile" within the VBA windo

----------


## macropod

> What do you mean by "how do you run the code"?



As in, are you running this from within Word, or from some other application?

----------


## gonecrawfishin

From within Word.  Thanks!

----------


## macropod

It appears I missed a variable declaration for Rng, but I don't see why that would cause the error you're reporting.

After:
wdDoc As Document
add:
, Rng As Range

----------


## gonecrawfishin

That did the trick, good eye.  It works perfectly for me now.  Thanks!

----------


## Severnfisher

> As coded, it processes every possible range in a document, but the comments indicate what can be deleted to restrict processing to, say, just the headers.



Not sure what to delete, to make this work on just headers. The macro runs but nothing changes in my document headers

----------


## macropod

I'd have thought it fairly obvious you'd delete everything between ''Process everything except headers & footers' and ''Process headers & footers'.

As for why nothing changes, that really depends on what you're using on the line:
Fnd = "Find Text": Rep = "Replace Text"

----------


## Severnfisher

Ah, obvious now you explian, thanks for that, not very good with macros.
It was my error it didn't work, copied the text to replace from the header, but included a space by mistake.
Works perfectly now, saved me hours of work, so thank you so much!

----------


## tmilburn

So I've got the macro working, I'll select the folder with all the documents in it and I can watch in file explorer as it opens each file. However, it will perform the change on some of the files and not the rest. I move the updated files to another folder and run the macro again. Again, it will update some of the files it didn't the first round, but not all of them. I keep doing this until all the files have been updated.

Can anyone tell me what's going on and how to get it to do all the files with one run of the macro?

BTW, thanks for the code.

----------


## macropod

Unless the files have some sort of protection, the problem is most likely due to the fact the file you're running the macro from has been saved to the folder you're trying to process; as soon as it encounters and processes itself, the process terminates.

----------


## tmilburn

macropod,
Thanks for the feedback. The file I am running the macro from is in a different folder than the files I'm trying to update. One of the files was giving me a message along the lines of "The author has requested the file be opened read only...", but it was at the bottom of the list and I moved it into a different folder and updated it manually. The number of files it would process each time varied, the first time 12 files, then 5 files, then 8 files, then 3 files. The files are located in a folder on my local hard drive, not over the network on in the cloud. I just didn't know if maybe there was a specific reason it would do this.

----------


## macropod

Unless there are some protected files (or a mailmerge main document), there is nothing about the macro that would cause it to fail in that way. The way it is coded, it will otherwise process all files in the selected folder.

----------


## tmilburn

Thanks, I'll poke around and see if I can figure it out.

----------


## FDibbins

tmilburn welcome to the forum  :Smilie: 

Unfortunately _your post does not comply with Rule 2 of our Forum_ RULES. *Do not post a question in the thread of another member -- start your own thread.* 

If you feel an existing thread is particularly relevant to your need, provide a link to the other thread in your new thread. 

Old threads are often only monitored by the original participants.  New threads not only open you up to all possible participants again, they typically get faster response, too.

----------


## Pushpalathabh

Hi Could you please share the final code that worked for u?

----------


## macropod

The only modification to the code in post 2 is in post 10.

----------


## Pushpalathabh

I inserted the VBA below code in Word. I am able to select the folder with word documents, but nothing happens after that.. I need to change instances of 2018 to 2019. This is most probably in the footer and very rarely in the pages.

I don't have any knowledge about VBA code.

Sub UpdateDocuments()
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String, wdDoc As Document, Rng As Range
Dim Sctn As Section, HdFt As HeaderFooter, Fnd As String, Rep As String
Fnd = "Find Text": Rep = "Replace Text"
strFolder = GetFolder
If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "\*.doc", vbNormal)
While strFile <> ""
  Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
  With wdDoc
    'Process everything except headers & footers
    For Each Rng In .StoryRanges
      Select Case Rng.StoryType
        Case wdPrimaryFooterStory, wdFirstPageFooterStory, wdEvenPagesFooterStory, _
          wdPrimaryHeaderStory, wdFirstPageHeaderStory, wdEvenPagesHeaderStory
        Case Else
          Call RngFndRep(Rng, Fnd, Rep)
      End Select
    Next
    'Process headers & footers
    For Each Sctn In .Sections
      'Process headers
      For Each HdFt In Sctn.Headers
        With HdFt
          If .LinkToPrevious = False Then
            Call RngFndRep(HdFt.Range, Fnd, Rep)
          End If
        End With
      Next
      'Process footers
      For Each HdFt In Sctn.Footers
        With HdFt
          If .LinkToPrevious = False Then
            Call RngFndRep(HdFt.Range, Fnd, Rep)
          End If
        End With
      Next
    Next
    .Close SaveChanges:=True
  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

Sub RngFndRep(Rng As Range, Fnd As String, Rep As String)
With Rng.Find
  .ClearFormatting
  .Replacement.ClearFormatting
  .Format = False
  .Forward = True
  .Wrap = wdFindContinue
  .Text = Fnd
  .Replacement.Text = Rep
  .MatchCase = True
  .MatchAllWordForms = False
  .MatchWholeWord = False
  .MatchWildcards = False
  .Execute Replace:=wdReplaceAll
End With
End Sub

----------


## macropod

As is fairly obvious from the discussion earlier in this thread, the process looks for the strings defined in:
Fnd = "Find Text": Rep = "Replace Text"
It's unlikely whatever you're trying to replace is the string 'Find Text' or that you want to replace it with the string 'Replace Text', but that's what your code has...

PS: When posting code, please use the code tags, indicated by the # button on the posting menu. Without them, your code loses much of whatever structure it had.

----------


## FDibbins

Pushpalathabh welcome to the forum  :Smilie: 

Unfortunately _your post does not comply with Rule 4 of our Forum_ RULES. *Do not post a question in the thread of another member -- start your own thread.* 

If you feel an existing thread is particularly relevant to your need, provide a link to the other thread in your new thread. 

Old threads are often only monitored by the original participants.  New threads not only open you up to all possible participants again, they typically get faster response, too.

----------


## cheshire13

Thanks for the code!! 

I have the same problem as tmilburn. When I ran the code it only worked on some files and the number of files that works each time seems pretty random. Has anyone come across any potential solutions?

----------


## macropod

Kindly read post 25.

----------


## FDibbins

cheshire13 welcome to the forum  :Smilie: 

Perhaps you missed my post IMMEDIATELY above yours??

----------


## cheshire13

Oh lol. Sorry I completely missed the whole second page. Thanks!

----------


## FDibbins

No problem  :Smilie:

----------

