none
Issues Converting Microsoft Word Documents To Pdf RRS feed

  • Question

  • I am trying to convert Microsoft Word Documents to PDF and having some issues during testing. At the moment I am converting about 5 documents and then deleting the converted PDFs and running the conversion program again to simulate a live environment scenario where I may have to re-run it in case of a failure. During the second or subsequent runs, I am getting the following error.

    Sometimes it gives the following message:

    I want to run this program off the task scheduler without any user intervention. Therefore I want to avoid all these errors. I don't want the word document to open or throw any messages. I need to understand how to avoid these errors. I have tried different methods to close the word documents in the program but without success. Can anyone please help? I have given my code below:

    Imports System.IO
    Imports Microsoft.Office.Interop.Word
    
    Module Module1
        Sub Main()
    
            Dim WordApplication As New Microsoft.Office.Interop.Word.Application()
            Dim WordDocument As Microsoft.Office.Interop.Word.Document
            Dim Files As String() = Directory.GetFiles("D:\Convert", "*.doc*")
    
            For Each File As String In Files
    
                Try
    
                    WordDocument = WordApplication.Documents.Open(File)
                    Dim PdFile As String = Path.ChangeExtension(File, "pdf")
    
                    WordDocument.ExportAsFixedFormat(PdFile, WdExportFormat.wdExportFormatPDF)
    
                Catch ex As Exception
                    My.Computer.FileSystem.WriteAllText("D:\Convert\Logfile.txt", ex.Message & vbCrLf, True)
                End Try
    
            Next
    
            WordApplication.Quit()
            WordApplication = Nothing
    
        End Sub
    
    End Module
    Appreciate any help.


    • Edited by RNDN Monday, January 2, 2017 12:34 AM Inserted images
    Monday, January 2, 2017 12:25 AM

All replies

  • During the second or subsequent runs, I am getting the following error.

    You haven't included any code to close the WORD document.  Most likely the document is still open because WORD has thrown up a dialog in response to whatever Close procedure you are using.  You need to show how you are closing the document, and explain why you believe that WORD will always execute the Close without user intervention.

    Monday, January 2, 2017 1:01 AM
  • During the second or subsequent runs, I am getting the following error.

    You haven't included any code to close the WORD document.  Most likely the document is still open because WORD has thrown up a dialog in response to whatever Close procedure you are using.  You need to show how you are closing the document, and explain why you believe that WORD will always execute the Close without user intervention.


    This is my entire code. Had I known how to close the word document correctly I would have done that. I am new to Visual Basic. Any suggestions?
    Monday, January 2, 2017 11:52 AM
  • This is my entire code. Had I known how to close the word document correctly I would have done that. I am new to Visual Basic. Any suggestions?

    This has nothing to do with VB - it is the WORD object model that dictates the procedures you need to follow.

    See: How to: Programmatically Close Documents

    Word Object Model Overview.

    Monday, January 2, 2017 8:49 PM