Dynamics Nav with Custom Word Exports

There are many pages in Dynamics Nav that let you export your data into a Word or Excel file. These exports are useful for managers and people running the business. But what if you need something that is customer facing? In this tutorial we are going to create a C# .NET add-in that will allow us to take a Word template and dynamically populate it with data from Nav. This would be usual for menus, flyers, and other promotional advertising. This process can only be used on .docx files, since they are really just zip files.  The idea is to take the Word document template, unzip it, change some of the key words, and zip it back up.

Word Template

First, lets make our template (menu.docx):

wordTemplate

You could add a lot more to this. Make sure you save your document as a .docx and not just a .doc. The x is very important! The main idea is to have unique text you can search for to replace. Each “***MENUITEMx*** will be replaced by an item from Nav. You could also set this up to switch out images and other things, but for this post we are only going to focus on changing the text. If we change the extension on our word doc to .zip. You can unzip it and see all the individual files that make up the word document. The main file we are going to be working with is document.xml in a folder called “word”.

wordTemplate

We need to open this up to make sure everything is formatted properly since Word can add in a lot of things that will mess up are text searching. you will notice in the below image that MENUITEM1 is all together in one spot, but MENUITEM2 is in two different elements.

wordTemplate

We need to remove the <w:r> tag for the “***” and add those stars to the correct spot. If we didnt make these changes, our .NET code would not be able to find and fully replace all the text. The reason this happens is you may have copy and pasted text, or did some other action in between the letters, and Word broke it up into two different elements. Even though they look like one thing viewing it in Word. When ever you make a change in any of these documents, save a copy, zip everything back up, and try to open the document in Word again. If you make a mistake and Word can’t read the change you made, good luck fixing it, Word will not tell you whats wrong, it simply won’t open the file. So make sure you keep testing any small changes you do.

This is how we want the file to look in the end. Any text we are replacing is in one spot.

wordTemplate

C# .NET

Now that we have our Word document formatted properly and ready to go. We can now start on creating the add in that will replace those “***MENUITEMx*** parts. We need only 2 methods besides the constructor. One to add items to our menu. The other to generate the docx file.

We aren’t going to bother dealing with this as a proper XML file. All we have to do is find the strings and replace them. You could add in a lot of functionality here, change colors, move things around, even add in entire sections into the document. If you want to know more about how these documents are formatted you can look here (There is a lot of documentation, have fun!). Once we made all our changes to the document file. We want to save it in a new place, zip it up, and let the user know where they can find it.

C/AL

With the ad-in done we can now hook it up to Nav. First we are going to add in a table to keep track of our food items. We only need the item name itself, you could add in more fields if you had more things on your Word document.

Field NumberField NameData TypeLength
1ItemText30

We need 2 pages. The first is a List Page part so that we can see all food items that will be added to our Word document. The other will allow us to enter in the item text and the name of our document.

Pages

 

On our Menu page we need to add in the following global variables.

NameTypeSubType
MenudotnetRyan.Erb.Blog.Demo.Menu.'Ryan.Erb.Blog.Demo,
ItemTextText
OutputFileLocationText
OutputFileNameText

Next, add in two actions to the Menu page. The Add Item() action will add the text typed into the ItemText box to our menu list. The other action will create the Word document and tell the user where they can find the document.

PageActions

And you are done! Here is what the page looks like in action, and the resulting word document.

Result ResultDoc

Hopefully this will make your life a little easier when making similar promotional flyers, or menus for your customers. If you don’t like the standard versions Nav gives you, you can always make your own. This exact same technique can be used for Excel and PowerPoint files. The best part is that since these are Word documents you can always make any final tweaks in Word before you print them off or send them to your customers.

Leave a Comment