Linx Main Linx Help

Populating a Word Document

#1

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. *

Pre-requisites:

  • 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

The process is as follows (Using the attached demo solution: WordPopulate.lsoz (12.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.

1) Create Custom Type “Dictionary” (PlaceholderAndValue)

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

  1. 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 (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 CustomType created at the beginning of this process (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.

  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:

    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.*

  7. Once the ForEach.CustomType 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+"\"

    *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 process + “\”. This will be used as the root path of the extracted files.

  9. Now, create a List Type of Type = String, named ‘ListOfFiles’. Now add the below items to the List:
    image
    If you look at the “unzipped” files, they resemble the above structure:
    image
    This will be used in the “zipping” process 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:

    image

    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.

  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 CustomType “Dictionary” and our Word populating process, we will now need to combine these to achieve our goal.

  1. Create a new process

  2. Add a List Type of Type CustomType.PlaceholderAndValue

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

  4. Below the list, add the process created above ‘PopulateWordDocument’

  5. 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

    image
    image

    Now, run the process 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) :

image

The replacement process 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 ‘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, 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.

0 Likes