-2

I have successfully been able to convert a word doc to pdf - but I can't get the winword process to end.

 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

I've tried pretty much everything and have been scratching my head for the past few days - can you point me in the right direction?

YowE3K
  • 23,852
  • 7
  • 26
  • 40
Syed Abbas
  • 11
  • 8
  • 1
    Possible duplicate of [The proper way to dispose Excel com object using VB.NET?](https://stackoverflow.com/questions/10309365/the-proper-way-to-dispose-excel-com-object-using-vb-net) - in particular, please see [the answer by Govert](https://stackoverflow.com/a/38111107/1115360). – Andrew Morton Dec 07 '17 at 13:41
  • @AndrewMorton - unfortunately have tried all of that - but could not get the releaseObject to work - it is not recognized – Syed Abbas Dec 07 '17 at 14:21
  • If you look at the answer by Govert it shows you how to do it. Also, see [my answer](https://stackoverflow.com/a/45650494/1115360) to a different question which used Govert's answer. – Andrew Morton Dec 07 '17 at 14:54
  • https://stackoverflow.com/questions/15697282/application-not-quitting-after-calling-quit this might help you. – Trevor Dec 07 '17 at 21:17
  • @AndrewMorton I think I understand what you are saying correct me if I'm wrong i need to create a sub that first calls the sub that deals with the word document then, after finishing its workload it will call garbage collection – Syed Abbas Dec 08 '17 at 20:42
  • @SyedAbbas Yes, that way works in my experience. – Andrew Morton Dec 09 '17 at 15:57

1 Answers1

0

Have you tried a Using statement?

Try wrapping everything in this.

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

End Using
Sea Charp
  • 288
  • 1
  • 6
  • thank you for responding - I get an error on the End using line Invalidcast exception 0x80004002 unable to cas COM object type Microsoft.office.Interop.Word.applicationclass to interface type System.idisposable – Syed Abbas Dec 08 '17 at 17:13
  • OK, sorry for misleading you, it appears that it does not implement iDisposable. – Sea Charp Dec 08 '17 at 18:10