Sign in to follow this  
Followers 0
wmoerdijk

Problems with COM object

2 posts in this topic

Hello,

I am trying to write a script to convert a RTF document to PDF. For this I am using the COM reference available with the "PDFCreator" package. In the installation there are working examples using WSH but I do not want to be dependent on WSH for this to work. The problem is that when I convert the script it does not work any more. I will post the full working vbs file and my simplified attemt to do a similar thing. My example is just a tes tthat convert a "test.rtf" in the root folder to a "test.pdf" in the "temp" folder.

This VBS script works when WSH and PDFCreator are installed:

CODE
' Convert2PDF script

' Part of PDFCreator

' License: GPL

' Homepage: http://www.sf.net/projects/pdfcreator

' Version: 1.0.0.0

' Date: September, 1. 2005

' Author: Frank Heindörfer

' Comments: This script convert a printable file in a pdf-file using

' the com interface of PDFCreator.

Option Explicit

Const maxTime = 30 ' in seconds

Const sleepTime = 250 ' in milliseconds

Dim objArgs, ifname, fso, PDFCreator, DefaultPrinter, ReadyState, _

i, c, AppTitle, Scriptname, ScriptBasename

Set fso = CreateObject("Scripting.FileSystemObject")

Scriptname = fso.GetFileName(Wscript.ScriptFullname)

ScriptBasename = fso.GetFileName(Wscript.ScriptFullname)

AppTitle = "PDFCreator - " & ScriptBaseName

If CDbl(Replace(WScript.Version,".",",")) < 5.1 then

MsgBox "You need the ""Windows Scripting Host version 5.1"" or greater!", vbCritical + vbSystemModal, AppTitle

Wscript.Quit

End if

Set objArgs = WScript.Arguments

If objArgs.Count = 0 Then

MsgBox "Syntax: " & vbtab & Scriptname & " <Filename>" & vbcrlf & vbtab & "or use ""Drag and Drop""!", vbExclamation + vbSystemModal, AppTitle

WScript.Quit

End If

Set PDFCreator = Wscript.CreateObject("PDFCreator.clsPDFCreator", "PDFCreator_")

PDFCreator.cStart "/NoProcessingAtStartup"

With PDFCreator

.cOption("UseAutosave") = 1

.cOption("UseAutosaveDirectory") = 1

.cOption("AutosaveFormat") = 0 ' 0 = PDF

DefaultPrinter = .cDefaultprinter

.cDefaultprinter = "PDFCreator"

.cClearcache

End With

For i = 0 to objArgs.Count - 1

With PDFCreator

ifname = objArgs(i)

If Not fso.FileExists(ifname) Then

MsgBox "Can't find the file: " & ifname, vbExclamation + vbSystemModal, AppTitle

Exit For

End If

if Not .cIsPrintable(CStr(ifname)) Then

MsgBox "Converting: " & ifname & vbcrlf & vbcrlf & _

"An error is occured: File is not printable!", vbExclamation + vbSystemModal, AppTitle

WScript.Quit

End if

ReadyState = 0

.cOption("AutosaveDirectory") = fso.GetParentFolderName(ifname)

.cOption("AutosaveFilename") = fso.GetBaseName(ifname)

.cPrintfile cStr(ifname)

.cPrinterStop = false

c = 0

Do While (ReadyState = 0) and (c < (maxTime * 1000 / sleepTime))

c = c + 1

Wscript.Sleep sleepTime

Loop

If ReadyState = 0 then

MsgBox "Converting: " & ifname & vbcrlf & vbcrlf & _

"An error is occured: Time is up!", vbExclamation + vbSystemModal, AppTitle

Exit For

End If

End With

Next

With PDFCreator

.cDefaultprinter = DefaultPrinter

.cClearcache

WScript.Sleep 200

.cClose

End With

'--- PDFCreator events ---

Public Sub PDFCreator_eReady()

ReadyState = 1

End Sub

Public Sub PDFCreator_eError()

MsgBox "An error is occured!" & vbcrlf & vbcrlf & _

"Error [" & PDFCreator.cErrorDetail("Number") & "]: " & PDFcreator.cErrorDetail("Description"), vbCritical + vbSystemModal, AppTitle

Wscript.Quit

End Sub

but this AutoIt script does not:

CODE
; Convert2PDF script

;If $CmdLine[0] = 0 Then Exit(1)

$file = "c:\test.rtf"

;Constants

$maxTime = 30 ; in seconds

$sleepTime = 250 ; in milliseconds

;Event handler

Func PDFCreator_eReady()

$ReadyState = 1

EndFunc

; init PDFCreator

$oPDFC = ObjCreate("PDFCreator.clsPDFCreator")

$ok = ObjEvent($oPDFC,"PDFCreator_")

$oPDFC.cStart "/NoProcessingAtStartup"

$oPDFC.cOption("UseAutosave") = 1

$oPDFC.cOption("UseAutosaveDirectory") = 1

$oPDFC.cOption("AutosaveFormat") = 0

$DefaultPrinter = $oPDFC.cDefaultprinter

$oPDFC.cDefaultprinter = "PDFCreator"

$oPDFC.cClearCache

$ReadyState = 0

$sOutFname = "test"

$sOutPath = "c:\temp\"

$oPDFC.cOption("AutosaveDirectory") = "c:\temp"

$oPDFC.cOption("AutosaveFilename") = "test"

$oPDFC.cPrintfile (String($file))

$oPDFC.cPrinterStop = false

$c = 0

While ($ReadyState = 0) and ($c < (30 * $maxTime / $sleepTime))

$c = $c + 1

Sleep (250)

WEnd

If $ReadyState = 0 then

MsgBox (1, "Error", "An error is occured: Time is up!")

EndIf

$oPDFC.cDefaultprinter = $DefaultPrinter

$oPDFC.cClearcache

$oPDFC.cClose

Sleep(1000); wait a second

It seems that the "cOption" setting I am trying do have no effect. What am I doing wrong?

Wouter.

Share this post


Link to post
Share on other sites



Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0