# Use ONLYOFFICE macros to easily add TextForms to master form templates

**URL:** https://community.onlyoffice.com/t/use-onlyoffice-macros-to-easily-add-textforms-to-master-form-templates/7510
**Category:** News
**Created:** [10 January 2024 09:49 UTC](https://community.onlyoffice.com/t/use-onlyoffice-macros-to-easily-add-textforms-to-master-form-templates/7510 "2024-01-10T09:49:03Z")
**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: [10 January 2024 09:49 UTC](https://community.onlyoffice.com/t/use-onlyoffice-macros-to-easily-add-textforms-to-master-form-templates/7510/1 "2024-01-10T09:49:03Z")

</div>

Originally published at: [Use ONLYOFFICE macros to easily add TextForms to master form templates | ONLYOFFICE Blog](https://www.onlyoffice.com/blog/2024/01/use-onlyoffice-macros-to-easily-add-textforms-to-master-form-templates/)

When working with form template documents, we often use dummy placeholders, and end up replacing them later with form elements. In this blog post, we will automate this process by using a [Document Editor](https://www.onlyoffice.com/desktop.aspx?utm_source=blog&utm_medium=article+&utm_id=insert_textform_macro) macro that inserts a TextForm at every instance of a specific word.

![Use ONLYOFFICE macros to easily add TextForms to master form templates](https://static-blog.onlyoffice.com/wp-content/uploads/2024/01/10124307/Insert-a-TextBox-at-a-particular-word-throughout-the-Doc.png)

## Building the macro

```
const oDocument = Api.GetDocument();
  const numberOfElements = oDocument.GetElementsCount();
```

Here, the current document is stored in the _oDocument_ variable. Then, we retrieve the total number of elements in the document using the _GetElementsCount()_ method.

```
for (let elementIndex = 0; elementIndex < numberOfElements; elementIndex++)
```

A _for loop_ is employed in this section to cycle through each _element_. By doing so, we make sure that we insert the TextForm for the specified word in every element (paragraph).

```
const oElement = oDocument.GetElement(elementIndex);
    const rawText = oElement.GetText();
```

Inside the _for loop_, we acquire each document element using the _GetElement()_ method with the _elementIndex_. Then, we extract the plain text, storing it in the _rawText_ variable.

```
const targetWord = "sample";
    let count = 0;
    for (let i = 0; i < targetWord.length; i++) {
      count++;
    }
    const wordLength = count - 1;
```

Here, we define the target word for our comments. Following that, we determine the length of the word using a _for loop_ and store it in the variable _wordLength_.

```
const wordRegex = new RegExp("\\b" + targetWord + "\\b", "gi");
```

Then, a regular expression (_wordRegex_) is created using the target word to find all occurrences in the document.

```
let match;
    while ((match = wordRegex.exec(rawText)) !== null) {
      let matchPosition = match.index; 
      let nextMatchPosition = matchPosition + wordLength;
```

Here, inside a _while loop,_ we iterate through all the instances found and stored in _wordRegex._

For each match, the starting position of the word _(matchPosition)_ and the position of the next character after the word (_nextMatchPosition)_ are calculated.

```
const insertRange = oElement.GetRange(matchPosition, nextMatchPosition);
insertRange.Select();
```

Following that, we store the range to operate in _insertRange_. Then, we select the _range_ using the _Select()_ method.

```
oDocument.InsertTextForm({
        key: "Personal information",
        tip: "Enter your first name",
        required: true,
        placeholder: "Name",
        comb: true,
        cellWidth: 3,
        multiLine: false,
        autoFit: false,
        placeholderFromSelection: true,
        keepSelectedTextInForm: false,
      });
```

Finally, we insert a _TextForm_ at the selected range. The _InsertTextForm()_ method has various parameters. You can modify them according to your needs.

## The full macro code

Here is the code for the entire macro.

```
(function () {
    function addTextForm() {
      const oDocument = Api.GetDocument();
      const numberOfElements = oDocument.GetElementsCount();

```
  //looping through all the elements in the document.
  for (
    let elementIndex = 0;
    elementIndex &lt; numberOfElements;
    elementIndex++
  ) {
    const oElement = oDocument.GetElement(elementIndex);
    const rawText = oElement.GetText();

    // Specify the target word and the length of the word
    const targetWord = "sample";
    let count = 0;
    for (let i = 0; i &lt; targetWord.length; i++) {
      count++;
    }
    const wordLength = count - 1;

    // Find all occurrences of the target word in the document
    const wordRegex = new RegExp("\\b" + targetWord + "\\b", "gi");
    let match;
    while ((match = wordRegex.exec(rawText)) !== null) {
      let matchPosition = match.index; // returns the index where the word starts.
      let nextMatchPosition = matchPosition + wordLength; //the index where the word ends.
      console.log(matchPosition);
      console.log(nextMatchPosition);
      const insertRange = oElement.GetRange(matchPosition, nextMatchPosition);
      insertRange.Select();
      //insert a text form at the selected range
      oDocument.InsertTextForm({
        key: "Personal information",
        tip: "Enter your first name",
        required: true,
        placeholder: "Name",
        comb: true,
        cellWidth: 3,
        multiLine: false,
        autoFit: false,
        placeholderFromSelection: true,
        keepSelectedTextInForm: false,
      });
    }
  }
}

addTextForm();

```

})();
```

Now, let us see how our macro performs.

https://www.youtube.com/embed/EvEzbUHg7es?si=E19_IrO72viRfbeq

We’re confident that integrating this macro into your oForm workflow will enhance your experience with Form Templates and oForms.

Don’t miss the chance to harness the power of the ONLYOFFICE API. Our extensive library of API methods is the 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=dev_blog&utm_medium=article+&utm_id=insert_textform_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

[Replace words macro for Document Editor](https://www.onlyoffice.com/blog/2023/11/replace-words-in-the-document-with-onlyoffice-macro?utm_source=dev_blog&utm_medium=article+&utm_id=insert_textform_macro)

[Macros in ONLYOFFICE Docs [FREE COURSE]](https://www.youtube.com/watch?v=drOa-uMpaPk&list=PLCF48HEKMOYNBygGjr9lSw3PLpVXQ6s_t&ab_channel=ONLYOFFICE)

[Macro Samples](https://api.onlyoffice.com/plugin/macrosamples/?utm_source=dev_blog&utm_medium=article+&utm_id=insert_textform_macro)

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

[ONLYOFFICE oForms API](https://api.onlyoffice.com/docbuilder/formapi?utm_source=dev_blog&utm_medium=article+&utm_id=insert_textform_macro)

[OFORMS library](https://oforms.onlyoffice.com/?utm_source=blog&utm_medium=article+&utm_id=insert_textform_macro)

[Other macros](https://www.onlyoffice.com/blog/search?s=macro&utm_source=dev_blog&utm_medium=article+&utm_id=insert_textform_macro)
