---
title: "How to Find and Replace in a Word document using VBA?"  
description: "How to Find and Replace in a Word document using VBA?"  
author: "Steilla Mitchel"  
published: 2023-07-19  
updated: 2023-07-19  
canonical: https://www.mindstick.com/forum/159153/how-to-find-and-replace-in-a-word-document-using-vba  
category: "ms office"  
tags: ["ms office"]  
reading_time: 3 minutes  

---

# How to Find and Replace in a Word document using VBA?

How to Find and [Replace](https://yourviews.mindstick.com/view/81330/cow-antibody-research-in-usa-a-step-ahead-to-replace-plasma-therapy) in a [Word document](https://www.mindstick.com/forum/1869/read-from-word-document-line-by-line) using [VBA](https://www.mindstick.com/forum/159637/how-to-avoid-using-select-in-excel-vba)?

## Replies

### Reply by Aryan Kumar

Sure, here is the code to find and replace in a [Word](https://www.mindstick.com/forum/305/read-word-file) [document](https://www.mindstick.com/articles/328642/what-to-consider-while-choosing-a-software-documentation-tool) using VBA:

```plaintext
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.


---

Original Source: https://www.mindstick.com/forum/159153/how-to-find-and-replace-in-a-word-document-using-vba

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
