2017-08-16 105 views
0

當試圖保存一個新的以編程方式創建.docx時,我不斷地在saveas,saveas2或保存調用時「命令失敗」。我知道該文件正在創建。離線工作正常。 的Visual Studio 2013 的Microsoft.Office.Interop.Word 15 的Microsoft Office 10服務器和開發機 測試代碼:Microsoft.Office.Interop.Word saveas2並保存使用vb.net獲取命令失敗

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 

回答

0

貌似你沒有設置的格式。試試這個代碼

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 
+0

謝謝你的回覆,但我仍然收到錯誤。我越是混淆了這一點,就越覺得它是服務器上的設置,iis或word。 – rtFord

0

我發現我的問題的解決方案,我正在尋找2天是在Word中的設置。 找到它的位置:https://social.msdn.microsoft.com/Forums/vstudio/en-US/53a0aa18-5f26-4a51-95c8-5c0ff9be28f4/command-failed-at-microsoftofficeinteropworddocumentclasssaveas?forum=vsto

「我之前遇到過這個問題,我瞪大眼睛了兩天,最後我發現,去運行命令DCOMCNFG.EXE或去組件服務的話,你必須選擇DCOM配置和解決方案選擇Microsoft Word並獲取它的屬性,並以標識選項卡選擇交互式用戶而不是啓動用戶。「