secuteamers
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 .PDFDistil ler.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.Fi leSystemOb ject")
Set wdo = CreateObject("Word.Applica tion")
Set wdocs = wdo.Documents
Amorph.L_status2.Caption = "Converting file: " & sDocFile
sTempFile = fso.GetSpecialFolder(Tempo raryFolder ) + "\" + fso.GetTempName()
sPDFPath = fso.GetAbsolutePathName(sP DFFile)
sDocPath = fso.GetAbsolutePathName(sD ocFile)
sPDFFolder = fso.GetParentFolderName(sP DFFile)
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.oldf ile)
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.BackgroundPrintingStat us > 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
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.
Set oDistiller = CreateObject("PDFDistiller
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.Fi
Set wdo = CreateObject("Word.Applica
Set wdocs = wdo.Documents
Amorph.L_status2.Caption = "Converting file: " & sDocFile
sTempFile = fso.GetSpecialFolder(Tempo
sPDFPath = fso.GetAbsolutePathName(sP
sDocPath = fso.GetAbsolutePathName(sD
sPDFFolder = fso.GetParentFolderName(sP
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.oldf
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.BackgroundPrintingStat
' '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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Thank you for the points!
hi try this
Remove this commenting from ur code
>> 'Set oDistiller = Nothing
Remove this commenting from ur code
>> 'Set oDistiller = Nothing
ASKER
thx