Link to home
Start Free TrialLog in
Avatar of nick_2007
nick_2007

asked on

Make PDF using CutePDF via VB

Having never worked with Visual Basic or VB Script I am at a loss on how this should all work.

I have installed the CutePDF printer driver and I would like to be able to convert document via the command line.

I have attached the following code which cutePDF provide for command line interfacing.

When I save it as a vbs file it fails on the AS variables and various other things.

I have managed to get wordapp.Documents.Open("C:\Sample.doc")  to work by calling the word document class.

I understand VBScript it a lite version of Visual Basic, but I was wondering if there was a way to convert it?

Alternatively what do I need to add my windows XP configuration? I downloaded Windows Script 5.7 for Windows XP but it still does not work.
Private Sub Print_PDF() 
 
     Dim lRetVal As Long 
     Dim hKey As Long 
     Dim sValue As String 
     lRetVal = RegCreateKeyEx(HKEY_CURRENT_USER, "Software\Custom PDF Printer",  _ 
                    0&, vbNullString, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, _ 
                    0&, hKey, lRetVal) 
     sValue = "C:\Sample.pdf" 
     RegSetValueExString hKey, "OutputFile", 0&, REG_SZ, sValue, Len(sValue) 
     sValue = "1" 
     RegSetValueExString hKey, "BypassSaveAs", 0&, REG_SZ, sValue, Len(sValue) 
 
     Dim worddoc As Word.Document 
     Set worddoc = wordapp.Documents.Open("C:\Sample.doc") 
     wordapp.ActivePrinter = "Custom PDF Printer" 
     wordapp.PrintOut 
     worddoc.Close 
 
     sValue = "0" 
     RegSetValueExString hKey, "BypassSaveAs", 0&, REG_SZ, sValue, Len(sValue)           
     RegCloseKey (hKey) 
 
End Sub

Open in new window

SOLUTION
Avatar of gregcmcse
gregcmcse
Flag of United States of America image

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 nick_2007
nick_2007

ASKER

I tried the code you supplied but it did not work for me, the error message was:

Wrong number of arguments or invalid property assignment: 'WSH'

Line 3 Char 9.

I assume it's correct to call it in the following way? With Print_PDF() being included at the bottom of the file?
Sub Print_PDF() 
 
        Set WSH = CreateObject("WScript.Shell")
        
        strPDFfile = "C:\Sample.pdf" 
        
        WSH.RegWrite "HKCU\Software\Custom PDF Printer\OutputFile", strPDFfile
        WSH.RegWrite "HKCU\Software\Custom PDF Printer\BypassSaveAs", "1"
        
        Dim wordapp, worddoc
        Set wordapp = CreateObject("Word.Application")
        Set worddoc = wordapp.Documents.Open(strPDFfile)
        wordapp.ActivePrinter = "Custom PDF Printer" 
        wordapp.PrintOut 
        worddoc.Close 
 
        WSH.RegWrite "HKCU\Software\Custom PDF Printer\BypassSaveAs", "0"
 
End Sub
 
Print_PDF() 

Open in new window

What I next tried was entering the required registry values in by hand since that was where the previous script was falling over.

How ever it then tell me ActivePrinter is not a supported method.

Having looked at: http://www.microsoft.com/technet/scriptcenter/resources/qanda/dec05/hey1206.mspx

I created the following which avoids using activePrint and sets the printer via altering the default.

This created a new problem with the Service WinWord not being shut down on completion.
strComputer = "."
 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
 
Set colPrinters =  objWMIService.ExecQuery _
    ("Select * from Win32_Printer Where Default = TRUE")
 
For Each objPrinter in colPrinters
    strOldDefault = objPrinter.Name
    strOldDefault = Replace(strOldDefault, "\", "\\")
Next
 
Set colPrinters =  objWMIService.ExecQuery _
    ("Select * from Win32_Printer Where Name = 'Custom PDF Printer'")
 
For Each objPrinter in colPrinters
    objPrinter.SetDefaultPrinter()
Next
 
Wscript.Sleep 2000
 
' Word need to be installed on this machine
Dim DocToPrint, oWord, oDoc
DocToPrint = "C:\test.docx"
Set oWord = CreateObject("Word.Application")
'Set oWordActiveDoc = oWord.Documents.Open("" & DocToPrint)
Set oWordActiveDoc = oWord.Documents.Open(DocToPrint)
'oWordActiveDoc.ActivePrinter = "Custom PDF Printer" 
oWordActiveDoc.PrintOut
oWordActiveDoc.close
 
 
Set colPrinters =  objWMIService.ExecQuery _
    ("Select * from Win32_Printer Where Name = '" & strOldDefault & "'")
 
For Each objPrinter in colPrinters
    objPrinter.SetDefaultPrinter()
Next

Open in new window

I've now got the following, which almost works except for it closes the word document before print is done.

How do I make it wait for the print to finish?
Dim DocToPrint, oWord, oDoc
DocToPrint = "C:/file.docx"
Set oWord = CreateObject("Word.Application")
Set oWordActiveDoc = oWord.Documents.Open("" & DocToPrint)
oWordActiveDoc.PrintOut
oWord.Quit
Set oWord = Nothing

Open in new window

ASKER CERTIFIED SOLUTION
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