开发者

Accessing and populating a Word 2007 document templates Quick Parts using C# and Microsoft.Office.Interop.Word?

I have created a document template file (.dotx) in Word 2007 and in this I have created a Quick Part. I 开发者_JAVA百科have been attempting to access this quick part programmatically using C# and the Microsoft.Office.Interop.Word namespace. Once I have access to the Quick Part I want to populate its fields with the properties of a POCO, then replicate this for all the POCO's in a List.

So far I have not been successful. Has anyone had any luck with this?

Note: I do not want to use the Open XML SDK if at all possible.


An alternative approach would be to populate a Custom XML part with the contents of your POCO.

The data would appear on the surface of the document via data bound content controls. A data bound content control identifies the data to which it is bound via XPath.

See generally http://msdn.microsoft.com/en-us/library/ff433638%28v=office.14%29.aspx


I found that I could access a Quick Part from a Document Template file using the following code:

    private BuildingBlock GetQuickPartFromTemplate(Microsoft.Office.Interop.Word.Application wordApplication, string wordTemplateDocumentPath, object quickPartName)
    {
        return (from Template template in wordApplication.Templates
                where template.FullName.Contains(Path.GetFileNameWithoutExtension(wordTemplateDocumentpath))
                let index = quickPartName
                select template.BuildingBlockEntries.Item(ref quickPartName)).FirstOrDefault();
    }

The first parameter is the Word Application, the second is the path of the Document Template file, while the third is the name of the Quick Part in the template.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜