【问题标题】:Closing word when using interop library - VB.net使用互操作库时的结束词 - VB.net
【发布时间】:2017-12-07 13:08:55
【问题描述】:

我已成功将 word doc 转换为 pdf - 但我无法结束 winword 进程。

 Dim wordApplication As Object = New Microsoft.Office.Interop.Word.Application
    Dim wordDocument As Object
    wordApplication.displayalerts = Word.WdAlertLevel.wdAlertsNone
    wordDocument = New Microsoft.Office.Interop.Word.Document
    'wordDocument = Nothing
    Dim outputFilename As String
    Dim filename As String

    filename = "c:\TestInvoice.doc"

    Try
        wordDocument = wordApplication.Documents.Open(filename, ReadOnly:=False)
        outputFilename = System.IO.Path.ChangeExtension(filename, "pdf")

        If Not wordDocument Is Nothing Then
            wordDocument.ExportAsFixedFormat(outputFilename, Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF, False, Microsoft.Office.Interop.Word.WdExportOptimizeFor.wdExportOptimizeForOnScreen, Microsoft.Office.Interop.Word.WdExportRange.wdExportAllDocument, 0, 0, Microsoft.Office.Interop.Word.WdExportItem.wdExportDocumentContent, True, True, Microsoft.Office.Interop.Word.WdExportCreateBookmarks.wdExportCreateNoBookmarks, True, True, False)
        End If
        'Threading.Thread.Sleep(3000)

        If File.Exists(System.IO.Path.ChangeExtension(filename, "pdf")) Then
            MessageBox.Show(System.IO.Path.ChangeExtension(filename, "pdf"))
        End If
        'wordDocument = Nothing

        'wordApplication.Documents(filename).Close(Word.WdSaveOptions.wdDoNotSaveChanges)

        'wordDocument.dispose()

        'wordApplication.quit(False)
        wordDocument.close()

        wordApplication.application.quit(False)
        wordApplication = Nothing

我已经尝试了几乎所有方法,并且在过去的几天里一直在摸不着头脑 - 你能指出我正确的方向吗?

【问题讨论】:

  • @AndrewMorton - 不幸的是已经尝试了所有这些 - 但无法让 releaseObject 工作 - 它无法识别
  • 如果您查看 Govert 的答案,它会告诉您如何操作。另外,请参阅 my answer 到另一个使用 Govert 答案的问题。
  • stackoverflow.com/questions/15697282/… 这可能会对你有所帮助。
  • @AndrewMorton 我想我明白你在说什么,如果我错了,请纠正我我需要创建一个子程序,它首先调用处理 word 文档的子程序,然后在完成其工作量后它将调用垃圾回收

标签: vb.net ms-word interop


【解决方案1】:

您是否尝试过 Using 语句?

试着把所有东西都包进去。

Using wordApplication As Object = New Microsoft.Office.Interop.Word.Application

End Using

【讨论】:

  • 感谢您的回复 - 我在 End using line Invalidcast exception 0x80004002 无法将 COM 对象类型 Microsoft.office.Interop.Word.applicationclass 转换为接口类型 System.idisposable 时收到错误
  • 好的,很抱歉误导您,它似乎没有实现 iDisposable。
猜你喜欢
  • 1970-01-01
  • 2023-04-04
  • 2012-05-16
  • 2015-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-24
  • 2017-06-16
相关资源
最近更新 更多