我想使用办公自动化打开一个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
有帮助吗?

解决方案

这里接受的答案可以是使用的:

操纵代码的文件(VB.NET)而不执行该文件的宏

其他提示

如果是的话,你正在使用2007个文档,请尝试更改您的代码直接与XML的工作,而不是使用办公自动化API。

它的速度更快,而且你不必担心宏(和许多其他自动化问题)。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top