Generating a .docx from a .dotx using merge (SimpleField) fields

Paul Aldred-Bann picture Paul Aldred-Bann · Mar 22, 2012 · Viewed 9.4k times · Source

So, first off here's my code to open the dotx and create a new docx copy (of which the copy is then modified). Cut for brevity, but essentially takes 3 params a data table (to make it usable by legacy systems), the UNC path as a string to a template and a UNC path as a string to the output document:

using (WordprocessingDocument docGenerated = WordprocessingDocument.Open(outputPath, true))
{
    docGenerated.ChangeDocumentType(WordprocessingDocumentType.Document);

    foreach (SimpleField field in docGenerated.MainDocumentPart.Document.Descendants<SimpleField>())
    {
        string mergeFieldName = GetFieldName(field).Trim();

        DataRow[] dr = dtSchema.Select("FieldName = '" + mergeFieldName + "'");

        if (dr.Length > 0)
        {
            string runProperties = string.Empty;
            foreach (RunProperties property in field.Descendants<RunProperties>())
            {
                runProperties = property.OuterXml;
                break;
            }

            Run run = new Run();
            run.Append(new RunProperties(runProperties));
            run.Append(new Text(dr[0]["FieldDataValue"].ToString()));

            field.Parent.ReplaceChild<SimpleField>(run, field);
        }
    }

    docGenerated.MainDocumentPart.Document.Save();
}

What I did initially was take a .dot template and re-save it as a .dotx and crossed my fingers, didn't work. So instead I tried deleting all merge fields in the .dotx and adding them again. This worked - but it would only find one merge field (as a SimpleField), specifically the last one added before saving the .dotx. Looking further at the template using the open XML productivity tool I can see that all other merge fields are of type w:instrText which is why they're being ignored.

I'm literally just starting out with OpenXML as we're looking to replace our current office automation with it so I know very little at this point. Could someone please instruct me a bit further or point me to a good resource? I've Google'd around a bit but I can't find my specific problem. I am trying to put off reading through the whole SDK documentation (I know, I know!) as I need to get a solution put together quickly so am focusing on a single task which is to take our existing .dot templates, convert them to .dotx and just replace merge fields with data to derive a .docx.

Thanks in advance!

Answer

Flowerking picture Flowerking · Mar 22, 2012

Working with OpenXml - you don't strictly need to use .dotx for your templates, instead you can make your templates just using DocX straight away. A good resource of learning OpenXML is obviously http://openxmldeveloper.org/ and you can find a good pdf read there.

Also worth looking at third party API docx.codeplex.com which I am using now for developing server side doc automation solution for my company. See the example http://cathalscorner.blogspot.co.uk/2009/08/docx-v1007-released.html which is similar to your scenario.. merging fields with data.. Hope this helps..