View Full Version : [SLEEPER:] Bulk Editing Multiple Document Footers In The Same Folder
Some1HelpPLz
04-02-2019, 11:00 AM
Hello, I'm sure this question has been asked in the past so I'm apologizing in advance, but I can't find a working solution and I've been searching for hours.
Problem: I have a folder filled with multiple word documents, and I need to update a certain portion of the footer section for all the documents in the folder. 
Example of what the footers look like:
Doc: 1 
Testing of HVAC Systems                                                       97 57 47 - 1
City of Colorado                                                                    3/8/2019
Doc: 2
Common Work Results                                                           99 23 22 - 1
City of Colorado                                                                     3/8/2019
Example of what I'm trying to have the footers changed to:
Doc: 1 
Testing of HVAC Systems                                                      97 57 47 - 1
City of New York                                                                  4/2/2019
Doc: 2
Common Work Results                                                         99 23 22 - 1
City of New York                                                                  4/2/2019
That's essentially all I'm trying to do, so I'm guessing what I'm really looking for is a find and replace function that works with footers. I've found related sources here and on the MSOfficeForum website but the codes never seems to work... For reference I'm using Word 2016 (not sure if that matters). Any and all help is appreciated, thank you! :crying:
macropod
04-02-2019, 02:41 PM
I've found related sources here and on the MSOfficeForum website but the codes never seems to work... 
That is most likely due to an implementation error. Beats me why you'd post here seeking help regarding code you've found on another forum - and give no indication of what that code is or how you've adapted it. Resources from there you could adapt include:
http://www.msofficeforums.com/word-vba/37236-loop-through-all-documents-folder.html#post121271
http://www.msofficeforums.com/word-vba/23916-help-creating-macro.html#post74951
http://www.msofficeforums.com/word-vba/29777-multi-doc-find-replace-including-headers-footers.html#post63290
Some1HelpPLz
04-02-2019, 04:16 PM
Beats me why you'd post here seeking help regarding code you've found on another forum - and give no indication of what that code is or how you've adapted it.
Hello Macropod, initially I was trying to provide links to the code I found, but the website wouldn't allow me to post links on my first post due to me having the "newbie" status. I'm guessing to deter spammers. I'm still trying to post links but it won't let me, I keep getting "Post denied. New posts are limited by number of URLs it may contain"
I wouldn't be surprised if it was due to an implementation error as I'm definitely no expert at this stuff. I've tried running all the code to the links you provided and each one of them will start with a pop up screen that will allow me to select a folder. Once I select a folder and press okay, nothing happens. Am I supposed to be modifying the code in someway to get it to do something to my footers?
The only code I could get to work was the following: (Sorry I can't post the actual link, the website won't allow me to, please replace (DOT) with "." and "h#t#m#l" with "html"
msofficeforums(DOT)com/word-vba/16209-run-macro-multiple-docx-files(DOT)h#t#m#l#post47274
They way I get the referenced code to work is by changing the .Text = " " and the, Replacement.Text = " ".
With this I can get the words in the body of the document themselves to be replaced, but for some reason no matter what I do I can't figure out how to get the words in the footer to be replaced...
Any guidance is greatly appreciated. Thanks!
macropod
04-02-2019, 04:36 PM
I wouldn't be surprised if it was due to an implementation error as I'm definitely no expert at this stuff. I've tried running all the code to the links you provided and each one of them will start with a pop up screen that will allow me to select a folder. Once I select a folder and press okay, nothing happens. Am I supposed to be modifying the code in someway to get it to do something to my footers?
The code in each of those links is written to solve a particular problem. None of them is written specifically for your problem so it's hardly surprising you don't get the results you're after without adapting the code to meet your particular requirements. I did, after all, say the code could be adapted...
For example, with the code in http://www.msofficeforums.com/word-vba/29777-multi-doc-find-replace-including-headers-footers.html#post63290, you'd delete the code between 'With wdDoc' and 'For Each Sctn In .Sections' and modify the Update(Rng As Range) sub to do the kind of Find/Replace you require. That would be as simple as changing:
  .Text = "04-15-09"
  .Replacement.Text = "05-05-14"
to:
  .Text = "[0-9]{1,2}/[0-9]{1,2}/[0-9]{4}"
  .Replacement.Text = Format(Now, "M/D/YYYY")
  .MatchWildcards = True
Some1HelpPLz
04-02-2019, 05:01 PM
Macropod, 
I've done as you instructed, and this is the result:
Sub UpdateDocuments()
    Application.ScreenUpdating = False
    Dim strFolder As String, strFile As String, wdDoc As Document
    Dim Sctn As Section, HdFt As HeaderFooter, Shp As Shape
    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
            For Each Sctn In .Sections
                For Each HdFt In Sctn.Headers
                    With HdFt
                        If .LinkToPrevious = False Then
                            ' Process the header
                            Call Update(.Range)
                        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 Update(Rng As Range)
    With Rng.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "[0-9]{1,2}/[0-9]{1,2}/[0-9]{4}"
        .Replacement.Text = Format(Now, "M/D/YYYY")
        .MatchWildcards = True
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .Execute Replace:=wdReplaceAll
    End With
End Sub
When I run the macro, it again produces a pop up window that allows me to select the folder I want, I press okay, and then anti-climatically... Nothing happens. No flickering screen of it running through all the documents, nada.
Any thoughts?
Thanks!
macropod
04-02-2019, 05:41 PM
Oops - one further change:-
Replace:
Sctn.Headers
with:
Sctn.Footers
PS: When posting code, please format it beforehand - just as the code you copied from MSOfficeForums was formatted.
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.