Print a webpage in Chrome

KarstenH

New Member
Joined
Aug 1, 2021
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi you Excel experts,

I'm looking for a solution to print a webpage in Chrome.

What I did so far with VBA:
1. I open Chrome and load a web page using Selenium
2. Enter credentials to the webpage to enter the website (using Selenium)
3. Enter some data and pushing a botton - both by using Selenium

As a next step I would like to print out the web page which is loaded after pushing the button in step 3 (please see above).

Do you have any idea how I can achieve this?


Thanks in advance for ypu help!

Best Regards
Karsten
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Hi Karsten. If you're using VBA to control Chrome via Selenium, I'm guessing that you are using the SeleniumBasic package. In that package, there were a number of sample scripts included to give some guidance on how to use to accomplish certain tasks - one of them being navigating to a website and then printing the webpage as a PDF. I have assumed that by "print out a webpage', you mean to save it as printoutable PDF file. If you need to also programmatically print it out, then that can be dealt with after obtaining a capture of the webpage.

Without seeing the code your using, it's tricky to give any more guidance, so I have reproduced the sample script below in it's entirety. Not all of it is relevant for your purposes, but let me know if you're having problems with it, and I can point out where in your code things need to be.

VBA Code:
' ---------------------------------------------------------------------------------------
' Search for "Eiffel tower", create a Pdf and insert a screen capture of the page result.
' ---------------------------------------------------------------------------------------

Class Script
    Dim driver

    Sub Class_Initialize
        Set driver = CreateObject("Selenium.ChromeDriver")
        Set pdf = CreateObject("Selenium.PdfFile")
        
        'Define the PDF page size and margins
        pdf.SetPageSize 210, 297, "mm"
        pdf.SetMargins 5, 5, 5, 15, "mm"
        
        'Add a title and some text to the PDF
        pdf.AddTextCenter "Search for Eiffel tower", 14, true
        pdf.AddSpace 10
        pdf.AddText "Lorem ipsum dolor sit amet, consectetur adipiscing elit. " & _
            "Etiam sit amet libero arcu, et molestie purus. Ut in sem lacus, " & _
            "sit amet rhoncus erat. In aliquet arcu at nunc porta sollicitudin. " & _
            "Cras ante nisl, hendrerit quis bibendum quis, egestas vitae mi."
        
        'Open the search page and maximize the window
        driver.Get "https://www.google.co.uk"
        driver.Window.Maximize
        
        'Add a title, the URL and a screenshot to the PDF
        pdf.AddTitle "Search page"
        pdf.AddLink driver.Url
        pdf.AddImage driver.TakeScreenshot()
        
        'Search for Eiffel tower
        driver.Get "/search?q=Eiffel+tower"
        
        'Add a title, the URL and a screenshot to the PDF
        pdf.AddTitle "Results page"
        pdf.AddLink driver.Url
        pdf.AddImage driver.TakeScreenshot()
        
        'Save the PDF to a file
        pdf.SaveAs "my-capture-ff.pdf"
    End Sub

    Sub Class_Terminate
        driver.Quit    'Stops the browser
    End Sub
End Class

Set s = New Script
 
Upvote 0
Solution
Hi Dan_W,

Thank you very much!
It works!
You made my day!


Best Regards
Karsten
 
Upvote 0

Forum statistics

Threads
1,214,639
Messages
6,120,679
Members
448,977
Latest member
dbonilla0331

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top