I continually get "Command Fail" at either the saveas, saveas2 or save calls when trying to save a new programmatically created .docx
. I know that the file is being created. works fine offline. Visual Studio 2013 Microsoft.Office.Interop.Word 15 Microsoft Office 10 on server and dev machine test code:
myInfo_lbl.Text = ""
Dim word As New Microsoft.Office.Interop.Word.Application
word.Visible = True
Dim doc As Microsoft.Office.Interop.Word.Document
doc = word.Documents.Add()
Try
Dim insertText As String = "This thing needs to start fn working. Damn it!"
Dim range As Microsoft.Office.Interop.Word.Range = doc.Range(Start:=0, End:=0)
range.Text = insertText
doc.SaveAs2("D:\myCVCOL_Files\test2.doc")
'doc.Save()
'doc.SaveAs2("D:\myCVCOL_Files\test2.doc")
Catch ex As COMException
myInfo_lbl.Text = ex.ErrorCode & " ~ " & ex.HResult & " ~ " & ex.Message & " ~ try 6"
Finally
Dim save_changes As Object = False
doc.Close(save_changes)
word.Quit(save_changes)
End Try
Looks like you didn't set the format. Try this code
Imports System.Runtime.InteropServices
Module Module1
Sub Main()
CreateDoc()
End Sub
Public Sub CreateDoc()
Dim wordApp As New Microsoft.Office.Interop.Word.Application
Dim wordDoc As Microsoft.Office.Interop.Word.Document
Dim range As Microsoft.Office.Interop.Word.Range
Dim fileName As String
Dim insertText As String
Dim format As Microsoft.Office.Interop.Word.WdSaveFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument
wordApp = New Microsoft.Office.Interop.Word.Application()
wordDoc = New Microsoft.Office.Interop.Word.Document()
insertText = "Some text"
wordApp.Visible = True
fileName = "D:\Projects\Sandbox\StackOverflow\ConsoleVB\test2.doc"
wordDoc = wordApp.Documents.Add()
range = wordDoc.Range(Start:=0, End:=0)
range.Text = insertText
wordDoc.SaveAs2(fileName, Format)
wordDoc.Close(Nothing, Nothing, Nothing)
wordDoc = Nothing
wordApp = Nothing
End Sub
End Module
I found out that the solution to my problem that I was searching for2 days for was a setting in Word. Found it at: https://social.msdn.microsoft.com/Forums/vstudio/en-US/53a0aa18-5f26-4a51-95c8-5c0ff9be28f4/command-failed-at-microsoftofficeinteropworddocumentclasssaveas?forum=vsto
"I faced this problem before and I googled it for two days and finally I found the solution that go to Run Command dcomcnfg.exe Or Go to Component Service Then you have to choose DCOM Config and choose Microsoft Word and get properties of it and in identity Tab choose the interactive User instead of the launching user."
I had for many months an 80% error when converting a word to a pdf using the function SaveAs(WordDocFilePath & WordDocName & ".pdf", Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF)
in VB.NET 2013 in a Windows 10 environment. I tried many things but this very simple change resolved just everything :
WordApp.ScreenUpdating = False (whether it was true before)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.