# Replace words in the document with ONLYOFFICE macro

**URL:** <https://community.onlyoffice.com/t/replace-words-in-the-document-with-onlyoffice-macro/6792>\
**Category:** News\
**Created:** [1 November 2023 08:19 UTC](https://community.onlyoffice.com/t/replace-words-in-the-document-with-onlyoffice-macro/6792 "2023-11-01T08:19:47Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![ONLYOFFICE\_team](https://community.onlyoffice.com/user_avatar/community.onlyoffice.com/onlyoffice_team/32/431_2.png) [@ONLYOFFICE\_team](https://community.onlyoffice.com/u/ONLYOFFICE_team)\
**Post date:** [1 November 2023 08:19 UTC](https://community.onlyoffice.com/t/replace-words-in-the-document-with-onlyoffice-macro/6792/1 "2023-11-01T08:19:47Z")

</div>

Originally published at: [Replace words in the document with ONLYOFFICE macro | ONLYOFFICE Blog](https://www.onlyoffice.com/blog/2023/11/replace-words-in-the-document-with-onlyoffice-macro/)

Document editing is a fundamental aspect of our daily tasks, and we frequently find ourselves yearning for tools that can ease the burden of repetitive chores. In thhttps://wpblog.onlyoffice.com/2023/11/replace-words-in…onlyoffice-macro/is blog post, we’ll build a macro designed to replace words in the document within a selection.

![Replace words in the document with ONLYOFFICE macro](https://static-blog.onlyoffice.com/wp-content/uploads/2023/11/01110920/replace_words.png)

## **Building the macro**

First, we access the current file in the document editor. Then, we target the selected text and capture the values. This range is where we intend to find and replace words:

```
const oDocument = Api.GetDocument();
const oRange = oDocument.GetRangeBySelect();
const rawText = oRange.GetText();
```

Then we delete the selected text:

```
oRange.Delete();
```

We assign the word we are looking for to the variable _wordToFind_, and the word we want to replace it with to the _replacementWord_:

```
// Define the word to find and the word to replace it with
const wordToFind = "oldWord"; // Replace "oldWord" with the word you want to find
const replacementWord = "newWord"; // Replace "newWord" with the word you want to replace it with
```

We apply a regular expression to the _rawText_ (the text within our selected range) to find all instances of _wordToFind_ and replace them with _replacementWord_. The regular expression with the “g” flag ensures that all occurrences are replaced. The modified text is stored in the _cleanedText_ variable:

```
// Use regular expression to find and replace the word
const cleanedText = rawText.replace(new RegExp(wordToFind, "g"), replacementWord);
```

Once the find-and-replace is complete, we create a new paragraph within the document using _Api.CreateParagraph()_. In this paragraph, we add text with the replaced words. Finally we insert the modified paragraph back into the document:

```
// Insert the cleanedText with the original paragraph structure
const oParagraph = Api.CreateParagraph();
oParagraph.AddText(cleanedText);
oDocument.InsertContent([oParagraph]);
```

The entire macro is the following:

```
(function()
{
const oDocument = Api.GetDocument();
const oRange = oDocument.GetRangeBySelect();
const rawText = oRange.GetText();
oRange.Delete();
// Define the word to find and the word to replace it with
const wordToFind = "oldWord"; // Replace "oldWord" with the word you want to find
const replacementWord = "newWord"; // Replace "newWord" with the word you want to replace it with
// Use regular expression to find and replace the word
const cleanedText = rawText.replace(new RegExp(wordToFind, "g"), replacementWord);
// Insert the cleanedText with the original paragraph structure
const oParagraph = Api.CreateParagraph();
oParagraph.AddText(cleanedText);
oDocument.InsertContent([oParagraph]);
})();
```

Now let’s run our macro and see how it works!

We hope that this macro will be a useful addition to your arsenal and streamline your document editing. The ONLYOFFICE macros are designed to elevate your workflow to new heights.

Don’t miss the chance to harness the power of the ONLYOFFICE API. Our extensive library of API methods is your key to transforming your ideas into reality. If you have any questions or innovative concepts, we encourage you to [share them with us](https://www.onlyoffice.com/contacts.aspx?utm_source=blog&utm_medium=article+&utm_id=replace_words_macro). Your input is highly valued, and we are excited about the possibility of collaborating with you. Best of luck in your exploratory endeavors!

### **Useful links**

[ONLYOFFICE Text Document API](https://api.onlyoffice.com/docbuilder/textdocumentapi?utm_source=blog&utm_medium=article+&utm_id=replace_words_macro)

[Macro samples](https://api.onlyoffice.com/plugin/macrosamples/?utm_source=blog&utm_medium=article+&utm_id=replace_words_macro)

[ONLYOFFICE on GitHub](https://github.com/ONLYOFFICE)

[Macros in ONLYOFFICE Docs (Free Course)](https://www.youtube.com/playlist?list=PLCF48HEKMOYNBygGjr9lSw3PLpVXQ6s_t)
