web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Generating word document from a word template with worker data.

(0) ShareShare
ReportReport
Posted on by 278

Hi All,

I have a requirement where we need to generate word documents using a word template (it will be designed using Dynamics Ax extension for word, dynamic fields shall be added. ).  The document templates will be uploaded to sharepoint which will be linked to AX. Now, the files will be generated from a batch job for selected workers fetching their data and writing into any selected template. 

I just need a hint on what to use for generating documents. I have gone through few number of websites, 

1. https://technet.microsoft.com/en-us/library/aa548928.aspx (it uses templates with bookmarks as field), but this won't be useful for bulk generation I guess since it can only be attached to a single document.

2.  http://daxguy.blogspot.com/2006/12/creating-ms-word-document-from-x.html (it just creates a word file, I cant see any mapping here).

Please help me on reaching for a effective solution.

Thanks,

-Vishal

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Hichem.Chekebkeb Profile Picture
    3,371 on at

    If I understand well your requirement, you can achieve that using .Net libraries. The most effective one is OpenXML..I have a small project I can share later. I used field controls using Developer mode in Word, gave them names than filled them using code.

  • Vishal Jevtani Profile Picture
    278 on at

    I guess that is what I am looking for. Please let me know when you will be able to share your small project or any reference just to give me an idea?

  • Suggested answer
    Hichem.Chekebkeb Profile Picture
    3,371 on at

    I planned to put it as a blog post but never-mind =)

    Below is the sample, the solution looks a bit ugly with too many IFs you can build a better design using OCP.

    I didn't use word templates I used normal doc and save as method instead.

    using DocumentFormat.OpenXml.Packaging;
    using DocumentFormat.OpenXml;
    using System.Collections;
     
    class SalaryCertificateTestClass
    {       
        /// <summary>
        /// Runs the class with the specified arguments.
        /// </summary>
        /// <param name = "_args">The specified arguments.</param>
        public static void main(Args _args)
        {   
        
            str                     FileToSave = @"\\Model\SalaryCertificateTemp.docx";
            str                     FileToSaveNEw = @"\\Instances\SalaryCertificateTempUpdated.docx";
          
     
            if (FileToSave && System.IO.File::Exists(FileToSave))
            {
                if(System.IO.File::Exists(FileToSaveNEw))
                    System.IO.File::Delete(FileToSaveNEw);
                System.IO.File::Copy(FileToSave,FileToSaveNEw);
            }
     
            using(WordprocessingDocument document =  WordprocessingDocument::Open(FileToSaveNEw, true))
            {
                document.ChangeDocumentType(WordprocessingDocumentType::Document);
                Wordprocessing.Text item = new DocumentFormat.OpenXml.Wordprocessing.Text();
                var allParas =  document.MainDocumentPart.Document.Descendants();
                var enum = allParas.getEnumerator();
                while (enum.moveNext())
                {
            
                    if(enum.Current.GetType().Equals(item.GetType()))
                    {
                        item = enum.Current;
                        if (item.Text == "TOWHOM")
                               item.Text = "Emirates NBD,DUBAI";
     
                        if (item.Text == "DATE")
                               item.Text = "13-Sep-2017";
     
                        if (item.Text == "NameAndNational")
                               item.Text = "Mr. Aleks , USA";
     
                        if (item.Text == "IdNumber")
                               item.Text = "123456789";
                        if (item.Text == "JoinDate")
                               item.Text = "02-Jan-2010";
                        if (item.Text == "Position")
                               item.Text = "Project Manager";
                        if (item.Text == "HisHer")
                               item.Text = "his";
                        if (item.Text == "Basic")
                               item.Text = "5000";
                        if (item.Text == "Total")
                               item.Text = "10000";
                      
                    }
                }
     
                document.Save();
                document.Close();
            }
     
            new Browser().navigate(FileToSaveNEw,true);
           
        }
     
    }

    Hope this is what you are looking for

  • Vishal Jevtani Profile Picture
    278 on at

    Hi Hichem,

    Thank you very much, this is exactly what I was looking for. :)

    I have written the same code in x++ and it seems to be working fine but I am having a strange issue. The line where you are fetching descendants (var allParas =  document.MainDocumentPart.Document.Descendants();), it does not executes the code. on generating CIL it gives a warning "Exception was thrown because the .NET instance method DocumentFormat.OpenXml.Wordprocessing.Document.Descendants was not found during CIL generation"

    Do you have any idea regarding this? I am opening a new question and marking your answer.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans