使用 .NET 和 Word 互操作,我以编程方式从模板 (.dot) 文件创建新的 Word 文档。有几种方法可以做到这一点,但我选择使用 AttachedTemplate 属性,例如:
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()
(由于在使用 Documents.Add() 从模板打开时发现了内存泄漏问题,我选择了 AttachedTemplate 方法而不是 Documents.Add() 方法。)
除非模板页脚中有图像(表示为 InlineShape),否则这可以正常工作。在这种情况下,图像不会出现在生成的文档中。具体来说,图像应该出现在oDoc.Sections.Item(1).Footers.Item(WdHeaderFooterIndex.wdHeaderFooterPrimary).Range.InlineShapes集合中,但它没有。
使用 Documents.Add() 时这不是问题,但是正如我所说的那样,该方法不是我的选择。
我是否需要采取额外的步骤才能从模板中获取图像?我已经发现,在使用 AttachedTemplate 时,我必须显式调用 UpdateStyles()(如您在我的代码片段中所见)将模板样式应用于文档,而使用 Documents.Add() 时会自动完成。或者也许有一些疯狂的解决方法?非常感谢您的帮助!:)