HTML to PDF through VBA using PDFCreator

前端 未结 1 675
太阳男子
太阳男子 2021-01-28 08:04

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

1条回答
  •  醉梦人生
    2021-01-28 08:42

    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
    

    0 讨论(0)
提交回复
热议问题