Link to home
Start Free TrialLog in
Avatar of secuteamers
secuteamersFlag for Belgium

asked on

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


ASKER CERTIFIED SOLUTION
Avatar of fulscher
fulscher

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of secuteamers

ASKER

Excellent. My app not only works, but i runs even faster than before. Here r ur 500 points.

thx
Avatar of fulscher
fulscher

Thank you for the points!
hi try this

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