使用互操作库时关闭word-VB.net

使用互操作库时关闭word-VB.net,vb.net,ms-word,interop,Vb.net,Ms Word,Interop,我已经成功地将word文档转换为pdf,但我无法结束winword进程 Dim wordApplication As Object = New Microsoft.Office.Interop.Word.Application Dim wordDocument As Object wordApplication.displayalerts = Word.WdAlertLevel.wdAlertsNone wordDocument = New Microsoft.Offi

我已经成功地将word文档转换为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

在过去的几天里,我几乎什么都试过了,一直在挠头——你能给我指出正确的方向吗?

你试过使用语句吗

试着把所有的东西都用这个包起来

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

End Using

可能的重复-特别是,请参见。@AndrewMorton-不幸的是,您已经尝试了所有这些-但无法使releaseObject工作-它无法识别。如果您查看Govert的答案,它会告诉您如何操作。另外,请注意使用Govert答案的另一个问题。这可能会对你有所帮助。@AndrewMorton我想我理解你的意思。纠正我,如果我错了,我需要创建一个sub,首先调用sub,然后处理word文档,完成其工作负载后,它将调用垃圾回收感谢您的响应-我在结尾处收到一个错误,使用行Invalidcast异常0x80004002无法将COM对象类型Microsoft.office.Interop.Word.applicationclass转换为接口类型System.idisposableOK,很抱歉误导您,它似乎没有实现iDisposable。