VBA Macro used in the Microsoft Office Excel Application

Excel Find and Replace across Multiple Word Documents | VBA Macro #30

Share on facebook
Share on twitter
Share on linkedin
Say a company rebranded and changed locations.  How would you handle updating all the necessary internal documents with the new company name, address, phone number, and email address? This can be a massive undertaking for any company which is why we created the Excel Find and Replace across Multiple Word Documents macro! This is an Excel macro that will Find and Replace values in Multiple Word Documents. This macro references 3 columns in Excel.  In Column A are the values to Find. In the adjacent cell in column B is the value that will Replace what is found.  Column C is optional allowing you an opportunity to populate the full path to any Word Documents you want to search through.  If nothing is populated in the Column C this macro will loop through all of the Word Documents available in the Active Excel Workbook’s Folder Path. Once a Word Document is opened this macro will loop through every Find and Replace value placed in Column A and Column B. This Excel macro is a great way to maintain Word Documents that are routinely changing information.

See it in Action!

Watch this video to see this macro in action.

The Code

Here is the code for this macro. Make sure the following References are setup before running it: Visual Basic For Applications, Microsoft Excel 16.0 Object Library, Microsoft Word 16.0 Object Library, Microsoft Office 16.0 Object Library, OLE Automation
'Leverage & Lean "Less Clicks, More Results"
Sub FindReplaceAcrossMultipleWordDocuments()
' Means variable is in use
Dim FindReplaceCounter As Integer '
Dim FolderPath As String '
Dim objWord As Object '
Dim oFile As Object '
Dim oFolder As Object '
Dim oFSO As Object '
Dim SearchDisplay As String '
Dim WordCounter As Integer '
Dim WordDocument As Object '
  
On Error GoTo LeverageLean

Set objWord = New Word.Application

If Cells(2, 3).Value <> "" Then 'If a path to Word Documents exist
WordCounter = 2
LastRow = Cells(Rows.Count, 3).End(xlUp).Row 'Identify Last Row in Column C
Do Until WordCounter > 4 'LastRow 'Loop through any Word Documents in Column C
Set WordDocument = objWord.Documents.Open(Cells(WordCounter, 3).Value)
objWord.Visible = True
SearchDisplay = SearchDisplay & ActiveDocument.Name & " Found and Replaced the following:" & vbNewLine
FindReplaceCounter = 2
LastRow = Cells(Rows.Count, 1).End(xlUp).Row 'Identify Last Row in Column A
Do Until FindReplaceCounter > LastRow 'Complete the Find and Replace for all values in Column A & B
With ActiveDocument.Content.Find
 .Execute FindText:=Cells(FindReplaceCounter, 1).Value, ReplaceWith:=Cells(FindReplaceCounter, 2).Value, _
 Format:=True, Replace:=wdReplaceAll
End With
SearchDisplay = SearchDisplay & Cells(FindReplaceCounter, 1).Value & " replaced " & Cells(FindReplaceCounter, 2).Value & vbNewLine
FindReplaceCounter = FindReplaceCounter + 1
Loop
SearchDisplay = SearchDisplay & "______________________________________________________" & vbNewLine & vbNewLine
ActiveDocument.Close SaveChanges:=wdSaveChanges 'Close Active Word Document
WordCounter = WordCounter + 1
Loop
ElseIf Cells(2, 3).Value = "" Then 'If NO paths to Word Documents exist
FolderPath = Left(ActiveWorkbook.FullName, Len(ActiveWorkbook.FullName) - Len(ActiveWorkbook.Name)) 'Active Workbook File Path
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(FolderPath)
For Each oFile In oFolder.Files 'Loop through every File in Active Workbook's folder path
If InStr(1, oFile.Type, "Microsoft Word") <> 0 And InStr(1, oFile.Name, "~") = 0 Then 'If the File Type contains the phrase Microsoft Word and is NOT Lock File
Set WordDocument = objWord.Documents.Open(FolderPath & oFile.Name)
objWord.Visible = True
SearchDisplay = SearchDisplay & ActiveDocument.Name & " Found and Replaced the following:" & vbNewLine
FindReplaceCounter = 2
LastRow = Cells(Rows.Count, 1).End(xlUp).Row 'Identify Last Row in Column A
Do Until FindReplaceCounter > LastRow 'Complete the Find and Replace for all values in Column A & B
With ActiveDocument.Content.Find
 .Execute FindText:=Cells(FindReplaceCounter, 1).Value, ReplaceWith:=Cells(FindReplaceCounter, 2).Value, _
 Format:=True, Replace:=wdReplaceAll
End With
SearchDisplay = SearchDisplay & Cells(FindReplaceCounter, 1).Value & " replaced " & Cells(FindReplaceCounter, 2).Value & vbNewLine
FindReplaceCounter = FindReplaceCounter + 1
Loop
SearchDisplay = SearchDisplay & "______________________________________________________" & vbNewLine & vbNewLine
ActiveDocument.Close SaveChanges:=wdSaveChanges 'Close Active Word Document
End If
Next oFile
End If

Word.Application.Quit SaveChanges:=wdPromptToSaveChanges

MsgBox SearchDisplay

Set objWord = Nothing
Set oFSO = Nothing
Set oFolder = Nothing
Set oFile = Nothing
Set WordDocument = Nothing
 
Exit Sub
 
LeverageLean:
MsgBox (Err.Number & " - " & Err.Description & vbNewLine & vbNewLine & "Don't hesitate to email me: brentschneider@leveragelean.com")
   
End Sub
'Stay Awesome

Macro Monday

Here is the Macro Monday video this macro was featured in. Watch this video to learn how to get the most out of this macro and start using it today!

Customization

These lines of code can be customized to personalize this macro.
66. MsgBox SearchDisplay Remove if you don’t want a MsgBox when this macro finishes
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments

Search for Excel Macros

Recent Macros

VBA Macro used in the Microsoft Office Outlook Application

Outlook Greeting and Goodbye Contacts | VBA Macro #35

The Greeting and Goodbye Contact macro automates the entry of a Greeting “Good Morning” or “Hello” followed by the First Name of the recipient you are emailing. To identify the First Name this macro uses the recipient’s email address to loop through all the existing contact records for a matching email address. If a match is found the contact’s First Name field value is used in the beginning of the email. The appropriate Greeting and Goodbye is determined by the current time and the day of the week. The Greeting and Goodbye Contacts macro helps you deliver better customer service and create better connections with your clients!

VBA Macro used in the Microsoft Office Excel Application

Excel Custom Questionnaire to File | VBA Macro #34

This is an Excel macro that generates a custom questionnaire to complete.  The Role field is used to determine what questions display for specific users.  Once a user selects their Role, they can click Generate to see what questions they need to complete to finish the questionnaire.  After a user has completed all the required questions, they can click Finish to create a separate Excel Workbook with all the populated answers.   You can customize where this new file is saved making it easy to know when someone has completed a questionnaire and is awaiting review. There is also the option to send this separate Excel Workbook in an Outlook email with a couple updates to the VBA code. There is always a better way with VBA and this Excel macro helps standardize the collection of answers to specific questions. This can help lead to a more formal review process and eliminate mistakes.

VBA Macro used in the Microsoft Office Outlook Application

Outlook Hello Contacts | VBA Macro #33

Starting an email with a Hello can look more personable with some VBA code! The Hello Contacts Outlook macro automates the entry of the text Hello followed by the First Name of the recipient you are emailing. This Outlook macro uses the recipient’s email address to loop through all the existing contact records for a matching email address. If a match is found the contact’s First Name field value is used in the beginning of the email. This macro can be fired from a custom button or trigger automatically when replying to an email.

First time using VBA?

The Developer Tab is an additional section of the ribbon when activated allows you access to Visual Basic in Applications like Excel, Outlook, Word.
Setup Now
Once the Developer Tab is available you will have the capability to update your security to allow Macros to run in the current application.
Setup Now
A sub of code is a collection of objects and variables. For the code to successfully run a library of references needs to be set.
Setup Now

Looking for more?

Subscribe as an Insider to receive additional rights.

Contact Us

Looking to improve your computer processes?
Leverage & Lean is here to help!

Copyright © 2019 Leverage & Lean – Uptown Style WordPress theme by GoDaddy