views:

136

answers:

1

Using .NET and the Word Interop I am programmatically creating a new Word doc from a template (.dot) file. There are a few ways to do this but I've chosen to use the AttachedTemplate property, as such:

Dim oWord As New Word.Application()
oWord.Visible = False

Dim oDocuments As Word.Documents = oWord.Documents

Dim oDoc As Word.Document = oDocuments.Add()
oDoc.AttachedTemplate = sTemplatePath
oDoc.UpdateStyles()

(I'm choosing the AttachedTemplate means of doing this over the Documents.Add() method because of a memory leak issue I discovered when using Documents.Add() to open from templates.)

This works fine EXCEPT when there is an image (represented as an InlineShape) in the template footer. In that case the image does not appear in the resulting document. Specifically the image should appear in the oDoc.Sections.Item(1).Footers.Item(WdHeaderFooterIndex.wdHeaderFooterPrimary).Range.InlineShapes collection but it does not.

This is not a problem when using Documents.Add(), however as I said that method is not an option for me.

Is there an extra step I have to take to get the images from the template? I already discovered that when using AttachedTemplate I have to explicitly call UpdateStyles() (as you can see in my code snippet) to apply the template styles to the document, whereas that is done automatically when using Documents.Add(). Or maybe there's some crazy workaround? Your help is much appreciated! :)

A: 

When only setting the AttachedTemplate property your newly created document will not inherited any content from the template at all. You will only get the styles and autotexts defined in the template and get access to the VBA macros defined in that template.

To really create a document based on a template you need (as you already described it) pass the template as a parameter to the Add() method.

Could you describe how this is leading to a memory leak, how you detected the leak, and what way this memory leak affects your application? It would be better to fix that problem (if it is really a leak) instead of using the workaround.

0xA3
In short I found that using Documents.Add() when specifying a template results in the WinWord process not correctly existing. (For more details check out the question and the selected answer in this post: http://stackoverflow.com/questions/2511464/using-word-com-objects-in-net-winword-process-wont-quit-after-calling-word-doc)
Keith