繁体   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