I'm pretty new to using the OpenXML SDK, so just trying to figure out the best way to automate some tasks.
I have a template .docx document and I want to setup some placeholders somehow and then once the process runs it will grab some information out of a database and put it into the word document in the place holders.
So basically I just need to do a find and replace out of a table and into the word document. I'm wondering where to start with doing this? I'm more familiar with Python, could I do it in that somehow?
Cheers!
Hi Weston,
Please have a look at this thread. You will get an idea to populate text within a placeholder.
Cool thanks for that. Where can I setup the WordP reference? as at the moment it says type or namespace could not be found.
using (WordprocessingDocument doc = WordprocessingDocument.Open("Test.docx", true)) { foreach (var cc in doc.ContentControls()) { WordP.SdtProperties props = cc.Elements<WordP.SdtProperties>().FirstOrDefault(); DocumentFormat.OpenXml.Wordprocessing.Tag tag = props.Elements<DocumentFormat.OpenXml.Wordprocessing.Tag>().FirstOrDefault(); Console.WriteLine(tag.Val); // adds content to only SdtBlock // perhaps you can maintain a list of tag names to check for the actual tag you are looking before populating some text in it. if (cc is WordP.SdtBlock) { WordP.Paragraph para = ((WordP.SdtContentBlock)((WordP.SdtBlock)cc).SdtContentBlock).GetFirstChild<WordP.Paragraph>(); WordP.Run run = para.GetFirstChild<WordP.Run>(); run.Append(new WordP.Text() { Text = "Inside " + tag.Val }); } } doc.MainDocumentPart.Document.Save(); }
Hi,
Sorry, I missed out WordP. Here it is,
using WordP = DocumentFormat.OpenXml.Wordprocessing;
An utility is available @ worddocgenerator.codeplex.com to generate Word documents from templates using Visual Studio 2010 and Open XML 2.0 SDK.