How do I programmatically open a MS Word document without invoking the Document_Open macro
-
21-08-2019 - |
Question
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
Solution
The accepted answer here may be of use:
Manipulate a file in code (VB.NET) without executing the file's macros
OTHER TIPS
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).
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow