Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

object required after 4th loop

Posted on 2005-04-12
4
Medium Priority
?
323 Views
Last Modified: 2013-12-26
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


0
Comment
Question by:secuteamers
  • 2
4 Comments
 
LVL 12

Accepted Solution

by:
fulscher earned 2000 total points
ID: 13764793
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
 

Author Comment

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

thx
0
 
LVL 12

Expert Comment

by:fulscher
ID: 13769840
Thank you for the points!
0
 
LVL 14

Expert Comment

by:Shiju Sasidharan
ID: 13769866
hi try this

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


0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question