Reputation: 6020
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!
Upvotes: 1
Views: 9953
Reputation: 149
Here is the link to "C# OpenXML Mail Merge Complete Example" http://www.jarredcapellman.com/2012/10/22/c-openxml-mail-merge-complete-example/
I used it to update my web application code that used to work with mergefields in .DOC (.DOT) files (and required MS Word to be installed and the corresponding DCOM to be configured on the web server). Now the solution requires just OpenXML SDK 2.0 to be installed on the web server and the .DOC (.DOT) templates to be saved as .DOCX (.DOTX).
Upvotes: 2
Reputation: 2132
If you want to use content controls you can also try WordDocumentGenerator. WordDocumentGenerator is an utility to generate Word documents from templates using Visual Studio 2010 and Open XML 2.0 SDK @ http://worddocgenerator.codeplex.com
Upvotes: 0
Reputation: 2581
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..
Upvotes: 4