To automatically print in excel but nothing happens

0 votes

In Excel, when I click a link, it prints out automatically, but nothing happens.

Mozilla is installed on my computer; the path is the same. Where might the root of this issue be? I'm attempting the following with this code:

Sub PrintWebPage()
    Dim ie As Object
    Dim url As String
    Dim cell As Range
    ' Check if a hyperlink is selected
    On Error Resume Next
    Set cell = ActiveCell.Hyperlinks(1).Range
    If cell Is Nothing Then
        MsgBox "Please select a hyperlink."
        Exit Sub
    End If
    On Error GoTo 0
    ' Get the URL from the hyperlink
    url = cell.Hyperlinks(1).Address
    ' Open the internet page in Google Chrome
    Set ie = CreateObject("InternetExplorer.Application")
    ie.Visible = True
    ie.Navigate url
    ' Wait for page to load
    Do While ie.Busy Or ie.readyState <> 4
        DoEvents
    Loop
    ' Print the page
    ie.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER
    ' Close Google Chrome
    ie.Quit
    Set ie = Nothing
End Sub
Feb 21, 2023 in Others by Kithuzzz
• 38,000 points
606 views

1 answer to this question.

0 votes

This is a revised version of your method that eliminates all potential errors. To troubleshoot any problems, you can also inspect the Instant Window in the VB editor.

Sub PrintWebPage_v2()

    'declarations
    Dim ie As Object
    Dim URL As String
    
    Debug.Print "Running routine."
    Debug.Print "Cell selected: " & ActiveCell.Address
    Debug.Print "Workbook: " & ActiveWorkbook.Name & " on sheet " & ActiveSheet.Name

    With ActiveCell
        
        'check hyperlink exists
        If .Hyperlinks.Count = 0 Then
            Debug.Print "No hyperlinks found. Quitting."
            MsgBox "Please select a hyperlink."
            Exit Sub
        End If
        
        'grab URL
        URL = .Hyperlinks(1).Address
        Debug.Print "Hyperlink found: " & URL
        
        'Open IE
        Set ie = CreateObject("InternetExplorer.Application")
        Debug.Print "IE opened."
        ie.Visible = True
        
        'Tell IE to open URL
        Debug.Print "Requesting URL"
        ie.Navigate URL
    
        'Allow IE to complete
        Do While ie.Busy Or ie.readyState <> 4
            DoEvents
        Loop
        Debug.Print "IE complete"
        
         ' Print the page
        ie.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER
        Debug.Print "Print command sent."
        
        ' Quit IE
        ie.Quit
        Debug.Print "IE closed."
        
    End With

End Sub 

The only part of the above I can't test is the ie.ExecWB OLECMDID_PRINT command. The rest works fine for me, opens a PDF etc.

answered Feb 21, 2023 by narikkadan
• 63,600 points

Related Questions In Others

0 votes
1 answer

Excel VBA : HOW TO PRINT THE TEXT IN A CELL (like Wrap Text)

Use a LineFeed character to create a ...READ MORE

answered Oct 27, 2022 in Others by narikkadan
• 63,600 points
1,030 views
0 votes
1 answer

How to merge columns in Excel but keep data on other columns

VLOOKUP indeed can be used here, combined ...READ MORE

answered Nov 19, 2022 in Others by narikkadan
• 63,600 points
965 views
0 votes
1 answer

In excel 365 how to subtract values sequentially but skip empty cells

Try the following formula in C4 and ...READ MORE

answered Dec 24, 2022 in Others by narikkadan
• 63,600 points
789 views
0 votes
1 answer

How to automatically get a specified range of data from Excel to XML in VBA

Range method works, always identify the sheet ...READ MORE

answered Mar 18, 2023 in Others by narikkadan
• 63,600 points
842 views
0 votes
1 answer

I Get "object required" error when using getElementByID

As you see LoginID is the id of the ...READ MORE

answered Jan 15, 2023 in Others by narikkadan
• 63,600 points
1,005 views
0 votes
1 answer

Retrieve epay.info Balance with VBA and Excel

This code should log you in, provided ...READ MORE

answered Sep 5, 2018 in Blockchain by digger
• 26,740 points
1,221 views
0 votes
1 answer

How to load file to Excel Power query from SFTP site

Currently, I don't think there is a ...READ MORE

answered Dec 3, 2018 in Power BI by Upasana
• 8,620 points
3,670 views
0 votes
1 answer

Using VBA Excel to create a gramatically correct list

The Excel AND function is a logical ...READ MORE

answered Feb 9, 2022 in Others by gaurav
• 23,260 points
917 views
0 votes
1 answer

How to print an Excel Sheet using VBA with Nitro PDF Creator in Excel 2016

you can use the built-in excel facilities ...READ MORE

answered Sep 24, 2022 in Others by narikkadan
• 63,600 points
1,352 views
0 votes
1 answer

Print chosen worksheets in excel files to pdf in python

In the simplest form: import win32com.client o = win32com.client.Dispatch("Excel.Application") o.Visible ...READ MORE

answered Sep 24, 2022 in Others by narikkadan
• 63,600 points
3,571 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP