# Populating a Word Document

**URL:** https://community.linx.software/community/t/populating-a-word-document/400
**Category:** How To
**Created:** [April 2, 2019, 3:42pm UTC](https://community.linx.software/community/t/populating-a-word-document/400 "2019-04-02T15:42:12Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![ronan](https://community.linx.software/community/user_avatar/community.linx.software/ronan/32/433_2.png) [@ronan](https://community.linx.software/community/u/ronan)
#### Post date: [April 2, 2019, 3:42pm UTC](https://community.linx.software/community/t/populating-a-word-document/400/1 "2019-04-02T15:42:12Z")

</div>

A scenario may arise where you have a Word document that you want to populate by utilizing Linx.

In Linx, there exists an Excel plugin which allows one to read/write excel spreadsheets using the built in functions, with Word documents however, there is not a built-in plugin but rather one must create a workaround in order to populate a Word document. The good news is that it is not required to have Microsoft Office/Word installed on the machine that Linx is running off.

 **The explanations are mostly for Linx5 users. We`ve included a sample for Linx6 users.**  
  
 Feel free to contact support@linx.software and we'll assist. 

  
Please note the terms ‘Process’ and ‘Custom Type’ have been depreciated and have been replaced with ‘Function’ and ‘Type’ respectively. More details [**here**](https://linx.software/docs/releasenotes/linx/5.20.2.0/).

> 💾 Sample **Solutions**  
> Linx 5.22.0 [WordPopulate.lsoz](https://community.linx.software/community/uploads/short-url/qrQSHKpXIukfTdyEbW0FFiqPQPG.lsoz) (9.2 KB)  
> Linx 6.0.5 [Linx6.zip](https://community.linx.software/community/uploads/short-url/nsfXqyxRf1SaF4mxBSCf81LfiAI.zip) (13.3 KB)  
> Sample word document [test.docx](https://community.linx.software/community/uploads/short-url/iU6Z5uGo5ewgncJgih2EbU6VStn.docx) (11.3 KB)

**Summary:**

- Unzip the Word document

- Search through the XML contents for placeholders and replace with values

- Zip the Word Document Files creating a new Word Document.

**Download the attached word document and change the path in the settings accordingly**

**1) Create Type “Dictionary” (PlaceholderAndValue)**

\*Note: Not specifically needed for this example but it demonstrates the functionality of replacing placeholders with values. \*

1. Create a _Type_ called ‘PlaceholderAndValue ‘ which will hold a Placeholder and Value, this will be used as a “dictionary”. The type will be looped through and depending on the Type. Placeholder string, if found in the doc, they will be replaced by the Type .Value.

**2) Create the Placeholder replacement Function([Linx 5.20.2.0](https://linx.software/docs/releasenotes/linx/5.20.2.0/)) (PopulateWordDocument)**

1. Using the _GenerateRandom_ function: Generate a “random” number which will then be converted into a string which will be used as the temporary file location for the Unzipped word document file to be generated to (see below).

2. Using the _Unzip_ function: Unzip the “template” location and extract the files to the _WordDocTemplateFullPath+RandomNumberGeneratedAbove.Base64String_. This will create a number of new files in the target location, one of them being the xml content of the wod document.

3. Using a _TextFileRead_ function: Read the file located at _WordDocTemplateFullPath+RandomNumberGeneratedAbove.Base64String + "\word\document.xml". For the ‘Read Type’, use ‘Complete’._

4. Assign the output of the _TextFileRead_ function to a _string type_ (In the example, it is named FileContents).

5. Using a _ForEach_ function: Assign the list to loop through as the list type of your Type created at the beginning of this Function([Linx 5.20.2.0](https://linx.software/docs/releasenotes/linx/5.20.2.0/)) (this can be an input parameter like the example, or it can be a Type list within the Function). This will be used to loop through all the possible placeholders and their corresponding values.

6. Within the _ForEach_ execution path, add a _SetValue_ function and set the Target = FileContents (String Type). For the source property, here you will do the replacing:

7. Once the ForEach.Type has completed, the FileContents type will contain the file contents with the replacement done.

8. For usability sake , you can then add a String Type , named ‘RootPath’, set the value of this to:  
_$.Input.DocxFileFullPath+GenerateRandomTempFolderName.Base64String+"\"_

9. Now, create a List Type of Type = String, named ‘ListOfFiles’. Now add the below items to the List:  
 ![image](https://community.linx.software/uploads/default/original/1X/63f47cd39625240226427b93344debc1dcbcf7ff.png)  
If you look at the “unzipped” files, they resemble the above structure:  
 ![image](https://community.linx.software/uploads/default/original/1X/c19806cba4048ecbc1a16bda88e3dd4a2d5edda2.png)  
This will be used in the “zipping” Function([Linx 5.20.2.0](https://linx.software/docs/releasenotes/linx/5.20.2.0/)) in the next step.\*\*

10. Using the Zip function, We must now, “zip” these files back again in order to have a complete Word Document. Configure the Zip function like below:

11. Finally, we need to clean up the files so there aren’t a bunch of unzipped files all over the place. To do this, simply use the DirectoryDelete function and point it to the ‘_$.Input.DocxFileFullPath+GenerateRandomTempFolderName.Base64String_’ location. This will then remove the unzipped Word document folder structure.

**3) Putting it all together (TestProcess)**  
So now we have made a Type “Dictionary” and our Word populating Function([Linx 5.20.2.0](https://linx.software/docs/releasenotes/linx/5.20.2.0/)) we will now need to combine these to achieve our goal.

1. Create a new Function

2. Add a List Type of Type Type.PlaceholderAndValue

3. Add items to the list which contain the placeholders of the document and their corresponding values.

4. Below the list, add the Function([Linx 5.20.2.0](https://linx.software/docs/releasenotes/linx/5.20.2.0/)) created above ‘PopulateWordDocument’

5. For the ‘PopulateWordDocument’, pass in the location of the template Word doc, the List of Type created in step (2) and then finally the file path of the result Word doc

![image](https://community.linx.software/uploads/default/original/1X/ec834310f8031dae4fe2c8df83d8689e390891d2.png)

The replacement Function([Linx 5.20.2.0](https://linx.software/docs/releasenotes/linx/5.20.2.0/)) is just to demonstrate the scenario of having a template word document which you might want to populate with Custom Names, Account balances etc.
