Knowledgebase: Developer
VBScript example of printing HTML to PDF
Posted by Craig Lebakken on 24 August 2010 03:48 PM
Question:
How can I convert an HTML document to PDF using VBScript?

Answer:

Sub SetPDFFile(strPDFFile)
   Const HKEY_LOCAL_MACHINE = &H80000002
   strKeyPath = "SOFTWARE\Dane Prairie Systems\Win2PDF"
   strComputer = "."
   Set objReg=GetObject( _
      "winmgmts:{impersonationLevel=impersonate}!\\" & _ 
      strComputer & "\root\default:StdRegProv")
   strValueName = "PDFFileName"
   objReg.SetExpandedStringValue HKEY_LOCAL_MACHINE,_
      strKeyPath,strValueName,strPDFFile
End Sub

Sub Print_HTML_Page(strPathToPage, strPDFFile)
   SetPDFFile( strPDFFile )
   Set objIE = CreateObject("InternetExplorer.Application")
   'From http://www.tek-tips.com/viewthread.cfm?qid=1092473&page=5
   'and http://msdn2.microsoft.com/en-us/library/aa752087.aspx
   On Error Resume Next
   strPrintStatus = objIE.QueryStatusWB(6)
   If Err.Number <> 0 Then
      MsgBox "Cannot find a printer. Operation aborted."
      objIE.Quit
      Set objIE = Nothing
      Exit Sub
   End If

   With objIE
      .visible=0
      .left=200
      .top=200
      .height=400
      .width=400
      .menubar=0
      .toolbar=1
      .statusBar=0
      .navigate strPathToPage
   End With

   'Wait until IE has finished loading
   Do while objIE.busy
      WScript.Sleep 100
   Loop

   On Error Goto 0
   objIE.ExecWB 6,2
   'Wait until IE has finished printing
   WScript.Sleep 2000
   objIE.Quit

   Set objIE = Nothing
End Sub
'=================
(5 vote(s))
Helpful
Not helpful

Comments (0)

Copyright 2000-2018 © Dane Prairie Systems LLC. All Rights Reserved. About Us | Privacy Policy | PDF Blog | Site Map