如何以编程方式打开 MS Word 文档而不调用 Document_Open 宏

发布于 2024-07-10 13:47:23 字数 1016 浏览 18 评论 0原文

我正在尝试使用 Office Automation 打开 Word 文档。 问题是我想在不调用 Document_Open 宏的情况下打开它。 有没有办法做到这一点?

下面的相关行是 wordApp.Documents.Open()

Imports Microsoft.Office.Interop

Public Class WordFunctions
  Public Shared Function ConvertToDoc(ByVal file As String) As Boolean
    Dim wordDoc As Word.Document
    Dim wordApp As Word.Application

    Try
        wordApp = CreateObject("Word.Application", "")
    Catch ex As Exception
        Return False
    End Try

    Try
        wordApp.Caption = "Automated Word Instance"
        wordApp.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone
        wordDoc = wordApp.Documents.Open(FileName:=file, Visible:=False, ConfirmConversions:=False)

        wordDoc.SaveAs(FileName:=file + ".doc", FileFormat:=Word.WdSaveFormat.wdFormatDocument)
        wordDoc.Activate()
        wordDoc.Close()
        Return True
    Catch ex As Exception
        Return False
    Finally
        wordApp.Quit(SaveChanges:=False)
    End Try
  End Function
End Class

I am trying to use Office Automation to open a word document. The problem is that I would like to open it without invoking the Document_Open macro. Is there a way to do this?

The relevant line below is wordApp.Documents.Open()

Imports Microsoft.Office.Interop

Public Class WordFunctions
  Public Shared Function ConvertToDoc(ByVal file As String) As Boolean
    Dim wordDoc As Word.Document
    Dim wordApp As Word.Application

    Try
        wordApp = CreateObject("Word.Application", "")
    Catch ex As Exception
        Return False
    End Try

    Try
        wordApp.Caption = "Automated Word Instance"
        wordApp.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone
        wordDoc = wordApp.Documents.Open(FileName:=file, Visible:=False, ConfirmConversions:=False)

        wordDoc.SaveAs(FileName:=file + ".doc", FileFormat:=Word.WdSaveFormat.wdFormatDocument)
        wordDoc.Activate()
        wordDoc.Close()
        Return True
    Catch ex As Exception
        Return False
    Finally
        wordApp.Quit(SaveChanges:=False)
    End Try
  End Function
End Class

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

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

发布评论

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

评论(1

∞梦里开花 2024-07-17 13:47:23

如果您正在使用 Word 2007 文档,请尝试更改代码以直接使用 XML,而不是使用 Office Automation API。

它更快,而且您不必担心宏(以及许多其他自动化问题)。

If it's Word 2007 documents you're working with, try changing your code to work with the XML directly, instead of using the Office Automation API.

It's faster, and you don't have to worry about macros (and lots of other automation issues).

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