Using Word COM objects in .NET, InlineShapes not copied from template to document
- by Keith
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! :)