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.
A Word document must exist (To get around having to setup a document each time, simply create a “template” and then use Linx to copy this template to the target location).
The Word document must have data inside it
Please note the terms ‘Process’ and ‘Custom Type’ have been depreciated and have been replaced with ‘Function’ and ‘Type’ respectively. More details here.
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.
1) Create Custom Type “Dictionary” (PlaceholderAndValue)
*Note: Not specifically needed for this example but it demonstrates the functionality of replacing placeholders with values. *
- Create a CustomType called ‘PlaceholderAndValue ‘ which will hold a Placeholder and Value, this will be used as a “dictionary”. The custom type will be looped through and depending on the CustomType.Placeholder string, if found in the doc, they will be replaced by the CustomType .Value.
2) Create the Placeholder replacement
process Function (PopulateWordDocument)
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).
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.
Using a TextFileRead function: Read the file located at WordDocTemplateFullPath+RandomNumberGeneratedAbove.Base64String + "\word\document.xml". For the ‘Read Type’, use ‘Complete’.
Assign the output of the TextFileRead function to a string type (In the example, it is named FileContents).
Using a ForEach function: Assign the list to loop through as the list type of your CustomType created at the beginning of this
processFunction (this can be an input parameter like the example, or it can be a CustomType list within the process). This will be used to loop through all the possible placeholders and their corresponding values.
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:
FileContents.Replace(ForEach.Loop.Placeholder, ForEach.Loop.Value.Replace("$.System.NewLine", " </w:t> <w:br/> <w:t> ").Replace("\n", " </w:t> <w:br/> <w:t> "))
What the above command will do is replace any placeholders in the file which match the current CustomType.Loop.Placeholder with the CustomType.Loop.Value. Furthermore it will take care of new line characters as well.*
Once the ForEach.CustomType has completed, the FileContents type will contain the file contents with the replacement done.
For usability sake , you can then add a String Type , named ‘RootPath’, set the value of this to:
*Note: It doesn’t necessarily have to be the input, but must achieve the goal of taking the initial Doc file path , concatenating it will the random string generated at the beginning of the
processFunction + “\”. This will be used as the root path of the extracted files.
Now, create a List Type of Type = String, named ‘ListOfFiles’. Now add the below items to the List:
If you look at the “unzipped” files, they resemble the above structure:
This will be used in the “zipping”
processFunction in the next step.**
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:
For the “Files to compress”, assign it to the List of files that you created earlier.
This will create a new word document in the supplier new file path.
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 CustomType “Dictionary” and our Word populating
process Function we will now need to combine these to achieve our goal.
Create a new process
Add a List Type of Type CustomType.PlaceholderAndValue
Add items to the list which contain the placeholders of the document and their corresponding values.
Below the list, add the
processFunction created above ‘PopulateWordDocument’
For the ‘PopulateWordDocument’, pass in the location of the template Word doc, the List of CustomType created in step (2) and then finally the file path of the result Word doc
Now, run the
processFunction and you should end up with a new Word document with the placeholders replaced. In the supplied example, you can see the difference between the Original (Left) and the New Word Document (right) :
process Function is just to demonstrate the scenario of having a template word document which you might want to populate with Custom Names, Account balances etc.
Using the same logic, you can create a whole chunk of text that you can populate the Word document with (Look at the sample solution
process Function ‘PopulateWordDocument_LargeChunk’). This can be done by having a base Word document as your template which just contains some type of placeholder, i.e. ‘#PlaceHolder#’. In the replacement process Function at step 2.6, instead of looping though the CustomType, you can simply use something like below:
TextFileRead.Replace("PlaceHolder#", FileContentsToPopulate.Replace("$.System.NewLine", " </w:t> <w:br/> <w:t> ").Replace("\n", " </w:t> <w:br/> <w:t> "))
The above allows a large chunk of pre-formatted text to be inserted into the Word Document.