如何在不调用Document_Open宏的情况下以编程方式打开MS Word文档

时间:2022-10-30 23:45:40

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?

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

The relevant line below is wordApp.Documents.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

2 个解决方案

#1


2  

The accepted answer here may be of use:

这里接受的答案可能有用:

Manipulate a file in code (VB.NET) without executing the file's macros

在不执行文件宏的情况下操作代码(VB.NET)中的文件

#2


0  

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.

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

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

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

#1


2  

The accepted answer here may be of use:

这里接受的答案可能有用:

Manipulate a file in code (VB.NET) without executing the file's macros

在不执行文件宏的情况下操作代码(VB.NET)中的文件

#2


0  

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.

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

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

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