VSTO:将多个部分粘贴到 Word 文档会生成虚拟宏

发布于 2024-07-19 12:55:24 字数 812 浏览 4 评论 0原文

我有一些 VSTO 代码,它通过复制和粘贴源文档中的选定部分来生成 Word 2003 文档:

    Dim doc = Globals.ThisDocument.Application.Documents.Add(DocumentType:=Word.WdNewDocumentType.wdNewBlankDocument, Visible:=False)
    For Each sectionNumber As Integer In requiredSections
        sourceDoc.Sections(sectionNumber).Range.Copy()
        doc.Bookmarks("\endofdoc").Range.Paste()
    Next
    doc.SaveAs(FileName:=(fileName), FileFormat:=(format), LockComments:=False, Password:="", AddToRecentFiles:=False)
    doc.Close(SaveChanges:=False)

这工作正常,但是当我打开生成的文档时,我收到宏安全警告对话框(或者文档在设计模式下打开) ,取决于设置)。 我根本看不到文档中的任何宏,例如使用 Word 中的宏管理器。

这导致的真正问题是,如果宏安全性设置为“高”,它会阻止从 shell 进行打印(代码在指定了 Print 动词的文档上调用 Process.Start,这是首选的打印机制,如下所示)它适用于许多文档类型)。

奇怪的是,只有在将多个部分粘贴到目标文档时才会出现此问题。

有人有主意吗? 谢谢。

I have some VSTO code which generates a Word 2003 document by copying and pasting selected sections from a source document:

    Dim doc = Globals.ThisDocument.Application.Documents.Add(DocumentType:=Word.WdNewDocumentType.wdNewBlankDocument, Visible:=False)
    For Each sectionNumber As Integer In requiredSections
        sourceDoc.Sections(sectionNumber).Range.Copy()
        doc.Bookmarks("\endofdoc").Range.Paste()
    Next
    doc.SaveAs(FileName:=(fileName), FileFormat:=(format), LockComments:=False, Password:="", AddToRecentFiles:=False)
    doc.Close(SaveChanges:=False)

This works fine, but when I open the resulting document, I get the macros security warning dialog box (or the doc opens in design mode, depending on settings). I can't see any macros in the document at all, e.g. with the Macro Organizer in Word.

The real problem that this is causing is that it prevents printing from the shell from working if macro security is set to "High" (the code is invoking Process.Start on the document with the Print verb specified, which is the preferred print mechanism as it works for many document types).

The weird thing is that the problem only occurs if more than one section is pasted to the target document.

Anyone have any ideas? Thanks.

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

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

发布评论

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

评论(1

醉生梦死 2024-07-26 12:55:24

两个小时,没有人帮我完成工作;-)

为了防止其他人遇到这个问题,我最终通过将文档保存为 RTF 来解决这个问题,这样就没有宏,shell 打印也可以顺利进行。

现在,我屏息等待有人报告 Word RTF 输出的限制。

Two hours, and no-one's done my work for me ;-)

Just in case anyone else has this problem, I eventually worked around it by saving the document as RTF so there are no macros and the shell print works without a hitch.

Now I wait with bated breath for someone to report limitations in the Word RTF output.

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