Use OpenXML for formatting in Microsoft Word with MergeFields

Category: open xml format sdk

Question

TimHLDE on Mon, 08 Aug 2016 13:10:00


Hi Developers,

I've got a similar problem like Harish in here (https://social.msdn.microsoft.com/Forums/windowsapps/en-US/80203cdd-52a8-4a1e-94ad-66d89f756f4d/insert-merge-field-in-word-using-open-xml?forum=oxmlsdk). But since nobody answered it and the original poster isn't paying attention to that thread anymore I would like to open a new one.

What I want to do:

In our company we're using the workflow engine K2 blackpearl. With this engine it is possible to run custom codes and code snippets. We need to generate a Microsoft Word doc(/docx)-File with the possiblity to use a custom template. Because the interface is very rudimental I want to use HTML to format the text. Since Word isn't able to understand HTML I'm converting it to OpenXML and writing it into the document. My problem is that I want to insert the text at some defined places in the document (therefore I want to use Mergefields but I am open to use sth. different if so. suggests sth. better). Does anybody know how to do that?

I've tried methods that are suggested (https://vivekcek.wordpress.com/2012/08/25/create-a-word-document-from-a-template-using-c-mail-merge/) and use a converter which you can see here (https://html2openxml.codeplex.com/documentation).

Many thanks so far

Tim

Replies

Edward8520 on Tue, 09 Aug 2016 06:34:07


Hi Tim,

>> My problem is that I want to insert the text at some defined places in the document (therefore I want to use Mergefields but I am open to use sth. different if so. suggests sth. better).

Do you mean you want to insert some html text in Merge Fields? Have you achieved insert html text in Word document? In my option, you could try to get the Run of Merge Field, and then insert the html part after or before the Run. For inserting a simple string, here is a simple code, you could replace the string with html string.

        public  void ChangeMainDocumentPart(MainDocumentPart mainDocumentPart1)
        {
            Document document1 = mainDocumentPart1.Document;

            Body body1=document1.GetFirstChild<Body>();

            Paragraph paragraph1=body1.Elements<Paragraph>().ElementAt(1);
            Paragraph paragraph2=body1.Elements<Paragraph>().ElementAt(2);

            Run run1=paragraph1.Elements<Run>().ElementAt(1);
            Run run2=paragraph1.Elements<Run>().ElementAt(6);
            Run run3=paragraph1.Elements<Run>().ElementAt(7);
            Run run4=paragraph1.Elements<Run>().ElementAt(8);
            Run run5=paragraph1.Elements<Run>().ElementAt(9);

            Run run6 = new Run(){ RsidRunAddition = "004C78B6" };

            RunProperties runProperties1 = new RunProperties();
            Color color1 = new Color(){ Val = "000000", ThemeColor = ThemeColorValues.Text1 };
            FontSize fontSize1 = new FontSize(){ Val = "18" };
            FontSizeComplexScript fontSizeComplexScript1 = new FontSizeComplexScript(){ Val = "18" };

            runProperties1.Append(color1);
            runProperties1.Append(fontSize1);
            runProperties1.Append(fontSizeComplexScript1);
            Text text1 = new Text(){ Space = SpaceProcessingModeValues.Preserve };
            text1.Text = "T1 ";

            run6.Append(runProperties1);
            run6.Append(text1);
            paragraph1.InsertBefore(run6,run1);          
        }

Best Regards,

Edward