Sure, here is the code to find and replace in a Worddocument using VBA:
Sub FindAndReplace()
'Declare variables
Dim doc As Word.Document
Dim findWhat As String
Dim replaceWith As String
Dim startAt As Integer
Dim found As Boolean
'Get the active document
Set doc = ActiveDocument
'Get the find what text
findWhat = InputBox("Enter the text you want to find:")
'Get the replace with text
replaceWith = InputBox("Enter the text you want to replace with:")
'Get the start at location
startAt = InputBox("Enter the location where you want to start the search:")
'Set the found flag to false
found = False
'Loop through all the words in the document
For Each word In doc.Words
'If the current word matches the find what text
If word.Text = findWhat Then
'Set the found flag to true
found = True
'Replace the word with the replace with text
word.Text = replaceWith
End If
Next word
'If the find what text was not found
If Not found Then
'Display an error message
MsgBox "The text you were looking for was not found."
End If
End Sub
This code first declares a few variables:
doc is a variable of type Word.Document that represents the active document.
findWhat is a variable of type String that contains the text you want to find.
replaceWith is a variable of type String that contains the text you want to replace the find what text with.
startAt is a variable of type Integer that contains the location in the document where you want to start the search.
found is a variable of type Boolean that indicates whether or not the find what text was found.
The code then gets the active document using the ActiveDocument property. It then gets the find what text from the user using the
InputBox function. It then gets the replace with text from the user using the
InputBox function. It then gets the start at location from the user using the
InputBox function.
The code then sets the found flag to false. It then loops through all the words in the document. For each word, the code checks if the word's text matches the find what text. If it does, the code sets the
found flag to true and replaces the word with the replace with text.
If the find what text was not found, the code displays an error message.
To use this code, you would first need to insert it into a new or existing Word VBA project. You would then need to run the code. When running the code, you would be prompted to enter the find what text, the replace with text, and the start at location. Once you have entered all of the information, the code will find and replace the text in the document.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Sure, here is the code to find and replace in a Word document using VBA:
This code first declares a few variables:
docis a variable of typeWord.Documentthat represents the active document.findWhatis a variable of typeStringthat contains the text you want to find.replaceWithis a variable of typeStringthat contains the text you want to replace the find what text with.startAtis a variable of typeIntegerthat contains the location in the document where you want to start the search.foundis a variable of typeBooleanthat indicates whether or not the find what text was found.The code then gets the active document using the
ActiveDocumentproperty. It then gets the find what text from the user using theInputBoxfunction. It then gets the replace with text from the user using theInputBoxfunction. It then gets the start at location from the user using theInputBoxfunction.The code then sets the
foundflag to false. It then loops through all the words in the document. For each word, the code checks if the word's text matches the find what text. If it does, the code sets thefoundflag to true and replaces the word with the replace with text.If the find what text was not found, the code displays an error message.
To use this code, you would first need to insert it into a new or existing Word VBA project. You would then need to run the code. When running the code, you would be prompted to enter the find what text, the replace with text, and the start at location. Once you have entered all of the information, the code will find and replace the text in the document.