+ Reply to Thread
Results 1 to 30 of 30

Macro to Batch/Mass Edit Headers/Footers on Multiple Word Documents (.docx) - Replace Text

  1. #1
    JamesFrames8
    Guest

    Question Macro to Batch/Mass Edit Headers/Footers on Multiple Word Documents (.docx) - Replace Text

    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

  2. #2
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Macro to Batch/Mass Edit Headers/Footers on Multiple Word Documents (.docx) - Replace

    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.
    Cheers,
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    JamesFrames8
    Guest

    Re: Macro to Batch/Mass Edit Headers/Footers on Multiple Word Documents (.docx) - Replace

    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?

  4. #4
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Macro to Batch/Mass Edit Headers/Footers on Multiple Word Documents (.docx) - Replace

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

  5. #5
    Registered User
    Join Date
    07-24-2014
    Location
    NY
    MS-Off Ver
    2010
    Posts
    4

    Re: Macro to Batch/Mass Edit Headers/Footers on Multiple Word Documents (.docx) - Replace

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

  6. #6
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Macro to Batch/Mass Edit Headers/Footers on Multiple Word Documents (.docx) - Replace

    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)?

  7. #7
    Registered User
    Join Date
    07-24-2014
    Location
    NY
    MS-Off Ver
    2010
    Posts
    4

    Re: Macro to Batch/Mass Edit Headers/Footers on Multiple Word Documents (.docx) - Replace

    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

  8. #8
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Macro to Batch/Mass Edit Headers/Footers on Multiple Word Documents (.docx) - Replace

    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?

  9. #9
    Registered User
    Join Date
    07-24-2014
    Location
    NY
    MS-Off Ver
    2010
    Posts
    4

    Re: Macro to Batch/Mass Edit Headers/Footers on Multiple Word Documents (.docx) - Replace

    From within Word. Thanks!

  10. #10
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Macro to Batch/Mass Edit Headers/Footers on Multiple Word Documents (.docx) - Replace

    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

  11. #11
    Registered User
    Join Date
    07-24-2014
    Location
    NY
    MS-Off Ver
    2010
    Posts
    4

    Re: Macro to Batch/Mass Edit Headers/Footers on Multiple Word Documents (.docx) - Replace

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

  12. #12
    Registered User
    Join Date
    05-12-2010
    Location
    Manchester, England
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: Macro to Batch/Mass Edit Headers/Footers on Multiple Word Documents (.docx) - Replace

    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

  13. #13
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Macro to Batch/Mass Edit Headers/Footers on Multiple Word Documents (.docx) - Replace

    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"

  14. #14
    Registered User
    Join Date
    05-12-2010
    Location
    Manchester, England
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: Macro to Batch/Mass Edit Headers/Footers on Multiple Word Documents (.docx) - Replace

    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!

  15. #15
    Registered User
    Join Date
    03-24-2017
    Location
    Texas
    MS-Off Ver
    Microsoft Office 365 ProPlus
    Posts
    3

    Re: Macro to Batch/Mass Edit Headers/Footers on Multiple Word Documents (.docx) - Replace

    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.

  16. #16
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Macro to Batch/Mass Edit Headers/Footers on Multiple Word Documents (.docx) - Replace

    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.

  17. #17
    Registered User
    Join Date
    03-24-2017
    Location
    Texas
    MS-Off Ver
    Microsoft Office 365 ProPlus
    Posts
    3

    Re: Macro to Batch/Mass Edit Headers/Footers on Multiple Word Documents (.docx) - Replace

    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.

  18. #18
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Macro to Batch/Mass Edit Headers/Footers on Multiple Word Documents (.docx) - Replace

    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.

  19. #19
    Registered User
    Join Date
    03-24-2017
    Location
    Texas
    MS-Off Ver
    Microsoft Office 365 ProPlus
    Posts
    3

    Re: Macro to Batch/Mass Edit Headers/Footers on Multiple Word Documents (.docx) - Replace

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

  20. #20
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,926

    Re: Macro to Batch/Mass Edit Headers/Footers on Multiple Word Documents (.docx) - Replace

    tmilburn welcome to the forum

    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.
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  21. #21
    Registered User
    Join Date
    02-05-2019
    Location
    Chennai
    MS-Off Ver
    Office 2016
    Posts
    5

    Re: Macro to Batch/Mass Edit Headers/Footers on Multiple Word Documents (.docx) - Replace

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

  22. #22
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Macro to Batch/Mass Edit Headers/Footers on Multiple Word Documents (.docx) - Replace

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

  23. #23
    Registered User
    Join Date
    02-05-2019
    Location
    Chennai
    MS-Off Ver
    Office 2016
    Posts
    5

    Re: Macro to Batch/Mass Edit Headers/Footers on Multiple Word Documents (.docx) - Replace

    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

  24. #24
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Macro to Batch/Mass Edit Headers/Footers on Multiple Word Documents (.docx) - Replace

    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.

  25. #25
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,926

    Re: Macro to Batch/Mass Edit Headers/Footers on Multiple Word Documents (.docx) - Replace

    Pushpalathabh welcome to the forum

    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.

  26. #26
    Registered User
    Join Date
    05-06-2019
    Location
    New York, NY
    MS-Off Ver
    2013
    Posts
    2

    Re: Macro to Batch/Mass Edit Headers/Footers on Multiple Word Documents (.docx) - Replace

    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?

  27. #27
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Macro to Batch/Mass Edit Headers/Footers on Multiple Word Documents (.docx) - Replace

    Kindly read post 25.

  28. #28
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,926

    Re: Macro to Batch/Mass Edit Headers/Footers on Multiple Word Documents (.docx) - Replace

    cheshire13 welcome to the forum

    Perhaps you missed my post IMMEDIATELY above yours??

  29. #29
    Registered User
    Join Date
    05-06-2019
    Location
    New York, NY
    MS-Off Ver
    2013
    Posts
    2

    Re: Macro to Batch/Mass Edit Headers/Footers on Multiple Word Documents (.docx) - Replace

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

  30. #30
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,926

    Re: Macro to Batch/Mass Edit Headers/Footers on Multiple Word Documents (.docx) - Replace

    No problem

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Batch convert .docx to .txt
    By Cunner in forum Word Programming / VBA / Macros
    Replies: 8
    Last Post: 10-21-2021, 05:26 AM
  2. Replies: 4
    Last Post: 06-22-2015, 10:18 PM
  3. find in excel replace in word: find/replace text in text boxes and headers
    By dean.rogers in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-10-2012, 12:40 PM
  4. Replies: 0
    Last Post: 08-16-2006, 11:15 AM
  5. Can I edit headers/footers for an entire workbook?
    By On Line All The Time in forum Excel General
    Replies: 2
    Last Post: 11-30-2005, 08:30 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1