簡體   English   中英

Microsoft.Office.Interop.Word saveas2 並使用 vb.net gets 命令保存失敗

[英]Microsoft.Office.Interop.Word saveas2 and save using vb.net gets Command failed

在嘗試保存以編程方式創建的新.docx時,我在 saveas、saveas2 或 save 調用中不斷收到“命令失敗”。 我知道正在創建文件。 離線工作正常。 Visual Studio 2013 Microsoft.Office.Interop.Word 15 Microsoft Office 10 服務器和開發機器測試代碼:

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

看起來你沒有設置格式。 試試這個代碼

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

我發現我搜索了 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 並獲取它的屬性,然后在身份選項卡選擇交互式用戶而不是啟動用戶。”

在 Windows 中的 VB.NET 2013 中使用function SaveAs(WordDocFilePath & WordDocName & ".pdf", Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF)將單詞轉換為 pdf 時,我有好幾個月了 80% 的錯誤10 環境。 我嘗試了很多東西,但這個非常簡單的改變解決了所有問題:

WordApp.ScreenUpdating = False(之前是否為真)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM