在 .NET 中使用 Word COM 对象,InlineShapes 未从模板复制到文档

发布于 2024-08-26 22:26:33 字数 1017 浏览 6 评论 0原文

使用 .NET 和 Word Interop,我以编程方式从模板 (.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()  

(我选择 AttachedTemplate 方法而不是 Documents.Add() 方法来执行此操作,因为 内存泄漏问题我在使用 Documents.Add() 从模板打开时发现。)

这工作得很好,除非模板页脚中有图像(表示为 InlineShape)。在这种情况下,图像不会出现在生成的文档中。具体来说,图像应出现在 oDoc.Sections.Item(1).Footers.Item(WdHeaderFooterIndex.wdHeaderFooterPrimary).Range.InlineShapes 集合中,但事实并非如此。

使用 Documents.Add() 时这不是问题,但是正如我所说,该方法不适合我。

我需要采取额外的步骤才能从模板中获取图像吗?我已经发现,在使用 AttachedTemplate 时,我必须显式调用 UpdateStyles() (如您在我的代码片段中看到的)将模板样式应用到文档,而在使用 Documents.Add() 时,这是自动完成的。或者也许有一些疯狂的解决方法?非常感谢您的帮助! :)

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! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

多彩岁月 2024-09-02 22:26:33

仅设置 AttachedTemplate 属性时,您新创建的文档根本不会继承模板中的任何内容。您只能获取模板中定义的样式和自动文本,并可以访问该模板中定义的 VBA 宏。

要真正基于模板创建文档,您需要(正如您已经描述的那样)将模板作为参数传递给 Add() 方法。

您能否描述一下这如何导致内存泄漏、您如何检测到泄漏以及这种内存泄漏如何影响您的应用程序?最好解决该问题(如果确实是泄漏)而不是使用解决方法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文