MS Word VBA add footer to all word documents in folder

This MS Word VBA tutorial explains how to add footer to all word documents in a folder automatically.

MS Word VBA add footer to all word documents in a folder

A couple of years ago, my manager asked me to add the keyword Public / Confidential / Restricted to all Word documents footers to meet compliance requirement (this is a common practice), I painfully did it manually for hundreds of documents. In fact it could have been done automatically. In some of my previous posts, I have demonstrated how to manipulate Excel files by looping through documents in the same folder with the help of FileSystemObject (FSO), the same can be done to Word documents.

First of all, you should be able to run FSO in Word 2013. If you fail to run FSO Object, open VBE (ALT+F11) > Tools > References > Check the box Microsoft Scripting Runtine

loop_through_workbook

 

VBA C0de – add footer to all word documents in a folder

Since my expertise is not in Word VBA and I haven’t done any research how others would write the code, I am sure there would be a more elegant way to write the code but anyway this will get the work done! What my Macro does is to loop through all word documents under test folder and word documents in the subfolders, then open each document, add footer, save and close the file.  Note that my Macro only loops through 1 level subfolder.

Press ALT+F11 in a new word document, insert a Module and paste the below

Public Sub addFooter()
    Dim FSO As Object
    Dim folder As Object, subfolder As Object
    Set FSO = CreateObject("Scripting.FileSystemObject")
    folderPath = "C:\Users\WYMAN\Desktop\test"    'define your folder path
    Set folder = FSO.GetFolder(folderPath)
        
    For Each wd In folder.Files
        If Left(wd.Name, 2) <> "~$" And (Right(wd.Name, 3) = "doc" Or Right(wd.Name, 4) = "docx" Or Right(wd.Name, 4) = "docm") Then
            Documents.Open(wd.Path).Activate
            ActiveDocument.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
            Selection.WholeStory
            Selection.Delete
            Selection.TypeText Text:="Confidential"
            Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
            ActiveDocument.ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
            ActiveDocument.Close wdSaveChanges
        End If
    Next

    For Each subfolder In folder.SubFolders
        For Each wd In subfolder.Files
            If Left(wd.Name, 2) <> "~$" And (Right(wd.Name, 3) = "doc" Or Right(wd.Name, 4) = "docx" Or Right(wd.Name, 4) = "docm") Then
            Documents.Open(wd.Path).Activate
            ActiveDocument.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
            Selection.WholeStory
            Selection.Delete
            Selection.TypeText Text:="Confidential"
            Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
            ActiveDocument.ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
            ActiveDocument.Close wdSaveChanges
            End If
        Next
    Next
End Sub

Example – add footer to all word documents in a folder

I have created a folder on my Desktop called test, within the folder I have two more subfolders, inside of them are other word documents.

add-footer-to-all-word-documents-01

 

After running the Macro, all the documents within the test folder will have a footer “Confidential” aligned to the left.

add-footer-to-all-word-documents-02

If you are trying to do other things other than adding footer, you can always record a Macro (Developer tab > Record Macro) to find out what the VBA code is for your desired actions, and then paste those code into my Macro.

 

Leave a Reply

Your email address will not be published.