user1028687
user1028687

Reputation: 1

HTML to PDF through VBA using PDFCreator

I have been trying to automate PDFCreator using VBA.

Can I automate the creation of PDF from HTML file opened in IE?

My search on the web gave me codes that work within Excel or Word but what I really want is that I will input a HTML file path to a VBA form and it should open, navigate the browser and print it to PDF.

I know how to control PDFCreator by VBA, but I am not sure how do I link IE to the PDFCreator printer.

Upvotes: 0

Views: 11717

Answers (1)

GTG
GTG

Reputation: 4954

You can automate IE to let it print documents to any printer, including PDFCreator.
You may also want to check this blog, it shows how to let PDFCreator skip the "save" dialog. I'm not an expert on PowerShell, so I won't try to convert this to VBA

'A function that uses IE to print the contents of Google.com to a PDF document
Sub printgoogle()
    Dim Explorer As Object
    Dim eQuery As Long 'return value type for QueryStatusWB
    Dim i As Integer
    Dim fTime As Single

    'See function below, to set the default printer to PDFCreator.  Note:  The user would probably be grateful if you checked to see what is the current default printer and set it back when finished printing
    SetDefaultPrinter "PDFCreator"

    'Connect to Internet Explorer
    Set Explorer = CreateObject("InternetExplorer.Application")
    'Open some document.  This is usually a file on your computer, but I use Google here for test purposes
    Explorer.Navigate "www.google.com"

TryAgain:
        'Wait for 2 seconds to let IE load the document
        fTime = Timer
        Do While fTime > Timer - 2
            DoEvents
        Loop
        eQuery = Explorer.QueryStatusWB(6)  'get print command status
        If eQuery And 2 Then
            Explorer.ExecWB 6, 2, "", ""   'Ok to Print? Then execute the Print (6) command, without displaying the print dialog (2)
            'Wait for 2 seconds while IE prints
            fTime = Timer
            Do While fTime > Timer - 2
                DoEvents
            Loop
        Else
            GoTo TryAgain
        End If

End Sub

'This function sets the Windows default printer to whatever printer you insert as parameter
Public Sub SetDefaultPrinter(ByVal printerName As String)
    Dim oSH As WshNetwork
    Set oSH = New WshNetwork
    oSH.SetDefaultPrinter printerName
    Set oSH = Nothing
End Sub

Upvotes: 2

Related Questions