I am trying to use this code for the following:
When I click on a link in Excel it automatically prints, but nothing happens.
I have mozilla installed, the path is the same. Where could be the cause of this thing?
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
the code i have posted above.
Here is a rework of your routine, this time without any error ignoring to confuse things. You can also check the Immediate Window in the VB editor to debug any issues.
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.