object required after 4th loop

I have a small app that converts  all the doc file in a folder to pdf files, using the distiller. After my 4th, or sometimes 3rd doc-conversion i get an error on the line

oDistiller.FileToPDF sTempFile, sPDFName, "Print"

The error is "object required". Sometimes the error is "remote machine is unavailable.....".

This is my full code:

Private Sub B_morph_Click()
a = Dir(ori & "\" & mydocs)
Do While a <> ""
    i = i + 1
    L_status.Caption = "Converting file " & i & " of " & teller
    Call DOC2PDF(ori & "\" & a, dest & "\" & a)
    a = Dir
    'MsgBox (a)
Loop
end sub

Function DOC2PDF(sDocFile, sPDFFile)

    Dim fso ' As FileSystemObject
    Dim wdo ' As Word.Application
    Dim wdoc ' As Word.Document
    Dim wdocs ' As Word.Documents
    Dim sPrevPrinter ' As String
    Dim oDistiller ' As PDFDistiller.PDFDistiller.1


    Set oDistiller = CreateObject("PDFDistiller.PDFDistiller.1")
    If oDistiller Is Nothing Then
          MsgBox ("Error: Cannot create PDF document. Adobe Acrobat Distiller is not available! Quiting...")
          Exit Function
    End If
 

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set wdo = CreateObject("Word.Application")
    Set wdocs = wdo.Documents
    Amorph.L_status2.Caption = "Converting file: " & sDocFile

    sTempFile = fso.GetSpecialFolder(TemporaryFolder) + "\" + fso.GetTempName()
    sPDFPath = fso.GetAbsolutePathName(sPDFFile)
    sDocPath = fso.GetAbsolutePathName(sDocFile)
    sPDFFolder = fso.GetParentFolderName(sPDFFile)
    sPDFName = sPDFFolder & "\" & fso.GetBaseName(sPDFPath) + ".pdf"
    sTempFile = App.Path + "\tmp\" + fso.GetBaseName(sPDFPath) + ".ps"

    ' Remember current active printer
    sPrevPrinter = wdo.ActivePrinter
   
    'loop while previous .ps is not converted
    If Amorph.oldfile <> "" Then
        i = 0
        Do While Not fso.fileExists(Amorph.oldfile)
        i = i + 1
        Amorph.L_teller.Caption = i
        'do nothing
        Loop
    End If

    'wdo.ActivePrinter = "Acrobat PDFWriter"
    wdo.ActivePrinter = "Acrobat Distiller"

    ' Open the Word document
    Set wdoc = wdocs.Open(sDocPath)

    ' Print the Word document to the Acrobat Distiller or writer - will generate a postscript (.ps) (temporary) file
    wdo.PrintOut False, , , sTempFile

    ' use "Acrobat PDFWriter"
    Do While wdo.BackgroundPrintingStatus > 0
    ' 'Do nothing - just wait for printing to finish before closing Word
    Loop

    'sluiten van Word
    wdoc.Close False 'WdDoNotSaveChanges
    wdo.ActivePrinter = sPrevPrinter 'reset default printer
    'wdo.Quit False 'WdDoNotSaveChanges
    'Set wdo = Nothing
   
    ' Distill the postscript file to PDF
    oDistiller.FileToPDF sTempFile, sPDFName, "Print"

    ' Delete the temporary postscript file...
    fso.DeleteFile (sTempFile)
   
    ' set old file to current
    Amorph.oldfile = sPDFName
 
    Amorph.L_status2.Caption = ""
    'Set oDistiller = Nothing
    Set fso = Nothing
End Function


secuteamersAsked:
Who is Participating?
 
fulscherCommented:
Errors of this type appear to happen if the OLE object in question has problems with memory allocation or is not properly removed from memory.

Try the following code. Also, you should add a DoEvents statement in every wait loop, for example:
    ' use "Acrobat PDFWriter"
    Do While wdo.BackgroundPrintingStatus > 0
        DoEvents ' avoid eating up CPU like mad
    Loop
This should allow your code to actually run faster than before.

<code>
Private Sub B_morph_Click()
    Dim oDistiller ' As PDFDistiller.PDFDistiller.1
    Set oDistiller = CreateObject("PDFDistiller.PDFDistiller.1")
    If oDistiller Is Nothing Then
          MsgBox ("Error: Cannot create PDF document. Adobe Acrobat Distiller is not available! Quiting...")
          Exit Function
    End If
 

a = Dir(ori & "\" & mydocs)
Do While a <> ""
    i = i + 1
    L_status.Caption = "Converting file " & i & " of " & teller
    Call DOC2PDF(ori & "\" & a, dest & "\" & a, oDistiller)
    a = Dir
    'MsgBox (a)
Loop
end sub

Function DOC2PDF(sDocFile, sPDFFile, oDistiller)

    Dim fso ' As FileSystemObject
    Dim wdo ' As Word.Application
    Dim wdoc ' As Word.Document
    Dim wdocs ' As Word.Documents
    Dim sPrevPrinter ' As String

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set wdo = CreateObject("Word.Application")
    Set wdocs = wdo.Documents
    Amorph.L_status2.Caption = "Converting file: " & sDocFile

   <rest of code>
End Function

<code/>
0
 
secuteamersAuthor Commented:
Excellent. My app not only works, but i runs even faster than before. Here r ur 500 points.

thx
0
 
fulscherCommented:
Thank you for the points!
0
 
Shiju SasidharanAssoc Project ManagerCommented:
hi try this

Remove this commenting from ur code
>>         'Set oDistiller = Nothing


0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.