I found an easier way to do it and it works perfectly even if you don’t know the path where the chrome is located.
First of all, you have to paste this code in the top of the module.
Option Explicit
Private pWebAddress As String
Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
(ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _
ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
After that you have to create this two modules:
Sub LoadExplorer()
LoadFile "Chrome.exe" ' Here you are executing the chrome. exe
End Sub
Sub LoadFile(FileName As String)
ShellExecute 0, "Open", FileName, "http://test.123", "", 1 ' You can change the URL.
End Sub
With this you will be able (if you want) to set a variable for the url or just leave it like hardcode.
Ps: It works perfectly for others browsers just changing «Chrome.exe» to opera, bing, etc.
Я нашел более простой способ сделать это, и он отлично работает, даже если вы не знаете, где находится хром.
Прежде всего, вы должны вставить этот код в верхнюю часть модуля.
Option Explicit
Private pWebAddress As String
Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
(ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _
ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
После этого вам нужно создать эти два модуля:
Sub LoadExplorer()
LoadFile "Chrome.exe" ' Here you are executing the chrome. exe
End Sub
Sub LoadFile(FileName As String)
ShellExecute 0, "Open", FileName, "http://test.123", "", 1 ' You can change the URL.
End Sub
Благодаря этому вы сможете (если хотите) установить переменную для URL-адреса или просто оставить ее как жесткий код.
Ps: Он отлично работает для других браузеров, просто меняя «Chrome.exe» на opera, bing и т. Д.
Another request we see in various forums is how to launch a URL in an alternate web browser and not use Internet Explorer or whatever the default browser happens to be on a given PC.
Automating FireFox
Luckily for us, FireFox accepts command line switches for such automation.
Below is a simple procedure that accepts the URL you wish to open and opens a new tab in FireFox to that URL.
'---------------------------------------------------------------------------------------
' Procedure : OpenURLInFF
' Author : Daniel Pineault, CARDA Consultants Inc.
' Website : http://www.cardaconsultants.com
' Purpose : Open a URL in FireFox
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
' (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
' Req'd Refs: Uses Late Binding, so none required
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sURL : URL to open in FifeFox
'
' Usage:
' ~~~~~~
' Call OpenURLInFF("http://www.google.ca")
' Call OpenURLInFF("devhut.net")
'
' Revision History:
' Rev Date(yyyy/mm/dd) Description
' **************************************************************************************
' 1 2014-11-13 Initial Release
' 2 2018-02-01 Updated Copyright under CC licensing
' Error trapped FireFox not installed
'---------------------------------------------------------------------------------------
Sub OpenURLInFF(ByVal sURL As String)
On Error GoTo Error_Handler
Dim WSHShell As Object
Dim sFFExe As String 'FF executable path/filename
'Determine the Path to FF executable
Set WSHShell = CreateObject("WScript.Shell")
sFFExe = WSHShell.RegRead("HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindowsCurrentVersionApp PathsFirefox.EXE")
'Open the URL
Shell """" & sFFExe & """" & " -new-tab """ & sURL & "", vbHide
Error_Handler_Exit:
On Error Resume Next
If Not WSHShell Is Nothing Then Set WSHShell = Nothing
Exit Sub
Error_Handler:
If Err.Number = -2147024894 Then
MsgBox "FireFox does not appear to be installed on this compter", _
vbInformation Or vbOKOnly, "Unable to open the requested URL"
Else
MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Source: OpenURLInFF" & vbCrLf & _
"Error Description: " & Err.Description & _
Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
, vbOKOnly + vbCritical, "An Error has Occurred!"
End If
Resume Error_Handler_Exit
End Sub
Using Chrome Instead
Chrome can similarly be automate but instead of -new-tab, it is simply -url
A Procedure to Control Them All!
Okay, it was bothering me to have to have an individual VBA procedure for each browser and thought to myself: “How hard can it be to have a single procedure to use any browser of my choosing?”. Well, it turns out that it isn’t that hard at all (noting a minor special case for Opera and Microsoft Edge)! Below is a simple procedure that, currently, will work with 6 of the most popular browsers.
- Internet Explorer
- FireFox
- Chrome
- Opera
- Microsoft Edge
- Brave
Enum BrowserName
'This Enum is part of Sub OpenURL()
' *** If changes are made here, update GetBrowserNameEnumValue()
iexplore = 1
firefox = 2
chrome = 3
opera = 4
msedge = 5
brave = 6
End Enum
'---------------------------------------------------------------------------------------
' Procedure : OpenURL
' Author : Daniel Pineault, CARDA Consultants Inc.
' Website : http://www.cardaconsultants.com
' Purpose : Open a URL in a browser
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
' (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
' Req'd Refs: Uses Late Binding, so none required
' Dependencies: BrowserName Enum, GetDefaultBrowser(), GetBrowserNameEnumValue()
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sURL : URL to open
' lBrowser : Optional, browser to be used to open the URL, if omitted, the system's
' default browser will be used
'
' Usage:
' ~~~~~~
' Call OpenURL("https://www.google.ca") 'will use the user's default browser
' Call OpenURL("https://www.google.ca", iexplore)
' Call OpenURL("devhut.net", chrome)
' Call OpenURL("msdn.com", firefox)
' Call OpenURL("google.ca", opera)
' Call OpenURL("https://www.google.ca", msedge)
' Call OpenURL("https://www.google.ca", brave)
'
' Revision History:
' Rev Date(yyyy/mm/dd) Description
' **************************************************************************************
' 1 2014-11-13 Initial Release
' 2 2018-02-01 Updated Copyright under CC licensing
' Error trapped FireFox not installed
' 3 2018-02-01 Complete revamp of the code to accomodate multiple
' Browser
' 4 2020-04-27 Added Microsoft Edge
' Added Brave
' 5 2020-12-14 Adapted to now have lBrowser as optional and the
' ability to determine the system's default browser
' 6 2022-07-03 Fixed usage examples to match Enum, forgot to do so
' after the last update
' changed msedge sExe to make people happy, not used!
'---------------------------------------------------------------------------------------
Sub OpenURL(ByVal sURL As String, Optional lBrowser As BrowserName)
Dim oShell As Object
Dim sFFExe As String 'Executable path/filename
Dim sProgName As String 'Name of the Executable program
Dim sExe As String 'Executable exe filename
Dim sCmdLineSwitch As String 'Command line switch
Dim sShellCmd As String 'Shell Command
On Error GoTo Error_Handler
'If no browser is specified then use the system's default one
If lBrowser = 0 Then
lBrowser = GetBrowserNameEnumValue(GetDefaultBrowser())
End If
'Determine the Path to executable
Select Case lBrowser
Case 1
'https://msdn.microsoft.com/en-us/library/hh826025(v=vs.85).aspx
sProgName = "Internet Explorer"
sExe = "IEXPLORE.EXE"
sCmdLineSwitch = " "
Case 2
'https://developer.mozilla.org/en-US/docs/Mozilla/Command_Line_Options#Browser
sProgName = "Mozilla Firefox"
sExe = "Firefox.EXE"
sCmdLineSwitch = " -new-tab "
Case 3
sProgName = "Google Chrome"
sExe = "Chrome.exe"
sCmdLineSwitch = " -tab "
Case 4
'http://www.opera.com/docs/switches/
sProgName = "Opera"
sExe = "opera.exe"
sCmdLineSwitch = " "
Case 5
sProgName = "Microsoft Edge"
sExe = "msedge.exe"
sCmdLineSwitch = " -tab "
Case 6
sProgName = "Brave"
sExe = "brave.exe"
sCmdLineSwitch = " -tab "
End Select
If lBrowser = 5 Then 'Special case for Edge! Thank you Microsoft for not following the rules!
'Build the command
sShellCmd = "cmd /c """ & "start microsoft-edge:" & sURL & """"
Else
Set oShell = CreateObject("WScript.Shell")
sFFExe = oShell.RegRead("HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindows" & _
"CurrentVersionApp Paths" & sExe & "")
'Parse the returned string
sFFExe = Replace(sFFExe, Chr(34), "") 'Special case for Opera?!
'Build the command
sShellCmd = """" & sFFExe & """" & "" & sCmdLineSwitch & """" & sURL & """"
End If
'Open the URL
Shell sShellCmd, vbHide
Error_Handler_Exit:
On Error Resume Next
If Not oShell Is Nothing Then Set oShell = Nothing
Exit Sub
Error_Handler:
If Err.Number = -2147024894 Then
MsgBox sProgName & " does not appear to be installed on this compter", _
vbInformation Or vbOKOnly, "Unable to open the requested URL"
Else
MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Source: OpenURL" & vbCrLf & _
"Error Description: " & Err.Description & _
Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
, vbOKOnly + vbCritical, "An Error has Occurred!"
End If
Resume Error_Handler_Exit
End Sub
'---------------------------------------------------------------------------------------
' Procedure : GetDefaultBrowser
' Author : Daniel Pineault, CARDA Consultants Inc.
' Website : http://www.cardaconsultants.com
' Purpose : Returns the name of the System's Default Web Browser
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
' (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
' Req'd Refs: Uses Late Binding, so none required
'
' Usage:
' ~~~~~~
' GetDefaultBrowser()
' -> msedge, firefox, brave, iexplore, ...
'
' Revision History:
' Rev Date(yyyy/mm/dd) Description
' **************************************************************************************
' 1 2020-12-14 Initial Release
'---------------------------------------------------------------------------------------
Function GetDefaultBrowser() As String
Dim oShell As Object
Dim sProgId As String
Dim sCommand As String
Dim aCommand As Variant
On Error GoTo Error_Handler
Set oShell = CreateObject("WScript.Shell")
'Default ProgId
sProgId = oShell.RegRead("HKEY_CURRENT_USERSoftwareMicrosoftWindowsShellAssociations" & _
"UrlAssociationshttpsUserChoiceProgId")
'Cross-reference the sProgId to get the exe associated with it
sCommand = oShell.RegRead("HKEY_CLASSES_ROOT" & sProgId & "shellopencommand")
'Parse the returned value to extract just the exe filename
aCommand = Split(sCommand, Chr(34))
GetDefaultBrowser = Right(aCommand(1), Len(aCommand(1)) - InStrRev(aCommand(1), "")) ' firefox.exe
GetDefaultBrowser = Left(GetDefaultBrowser, InStr(GetDefaultBrowser, ".") - 1) 'firefox
Error_Handler_Exit:
On Error Resume Next
If Not oShell Is Nothing Then Set oShell = Nothing
Exit Function
Error_Handler:
MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Source: GetDefaultBrowser" & vbCrLf & _
"Error Description: " & Err.Description & _
Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
, vbOKOnly + vbCritical, "An Error has Occurred!"
Resume Error_Handler_Exit
End Function
'---------------------------------------------------------------------------------------
' Procedure : GetBrowserNameEnumValue
' Author : Daniel Pineault, CARDA Consultants Inc.
' Website : http://www.cardaconsultants.com
' Purpose : Convert the returned value from GetDefaultBrowser() into the proper Enum
' Value. This is required as VBA offers no way to evaluate a returned
' value from a function against an Enum, no way to iterate over the string
' values of an Enum, ...
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
' (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
' Req'd Refs: None required
'
' Usage:
' ~~~~~~
' GetBrowserNameEnumValue(GetDefaultBrowser())
' -> 1, 2, 3, ...
' GetBrowserNameEnumValue("firefox")
' -> 2
'
' Revision History:
' Rev Date(yyyy/mm/dd) Description
' **************************************************************************************
' 1 2020-12-14 Initial Release
'---------------------------------------------------------------------------------------
Function GetBrowserNameEnumValue(sInput As String) As Long
On Error GoTo Error_Handler
Select Case sInput
Case "iexplore"
GetBrowserNameEnumValue = BrowserName.iexplore
Case "firefox"
GetBrowserNameEnumValue = BrowserName.firefox
Case "chrome"
GetBrowserNameEnumValue = BrowserName.chrome
Case "opera"
GetBrowserNameEnumValue = BrowserName.opera
Case "msedge"
GetBrowserNameEnumValue = BrowserName.msedge
Case "brave"
GetBrowserNameEnumValue = BrowserName.brave
Case Else
GetBrowserNameEnumValue = 0
End Select
Error_Handler_Exit:
On Error Resume Next
Exit Function
Error_Handler:
MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Source: GetBrowserNameEnumValue" & vbCrLf & _
"Error Description: " & Err.Description & _
Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
, vbOKOnly + vbCritical, "An Error has Occurred!"
Resume Error_Handler_Exit
End Function
Hello
I’m trying to import website tables to Excel and I have written a VBA code but it doesn’t work and I don’t have the idea why, also I want to make VBA to open on Chrome not in IE, below I will paste my code and if someones know, I would appreciate if he can make some changes.
Sub SIMPLEDATAEXTRACTION()
Dim IE As Object
Set IE = CreateObject(«Internetexplorer.Application»)
IE.Visible = True
IE.navigate «ti.com/data-converters/adc-circuit/products.html»
Do While IE.busy Or IE.readystate <> 4
DoEvents
Loop
ce = 1
For Each td In IE.document.getElementsbyTagName(«tr»)
Range(«A» & ce).Value = td.Children(0).innertext
Range(«B» & ce).Value = td.Children(1).innertext
Range(«C» & ce).Value = td.Children(2).innertext
Range(«D» & ce).Value = td.Children(3).innertext
Range(«E» & ce).Value = td.Children(4).innertext
Range(«F» & ce).Value = td.Children(5).innertext
Range(«G» & ce).Value = td.Children(6).innertext
Range(«H» & ce).Value = td.Children(7).innertext
Range(«I» & ce).Value = td.Children(8).innertext
Range(«J» & ce).Value = td.Children(9).innertext
Range(«K» & ce).Value = td.Children(10).innertext
Range(«L» & ce).Value = td.Children(11).innertext
Range(«M» & ce).Value = td.Children(12).innertext
Range(«N» & ce).Value = td.Children(13).innertext
ce = ce + 1
Next td
End Sub
I’m trying to open a Chrome browser from VBA. I understand Chrome does not support ActiveX settings so I’m curious if theres any work-arounds?
Dim ie As Object Set ie = CreateObject("ChromeTab.ChromeFrame") ie.Navigate "google.ca" ie.Visible = True
Answer
shell("C:UsersUSERNAMEAppDataLocalGoogleChromeApplicationChrome.exe -url http:google.ca")
Attribution
Source : Link , Question Author : Sam , Answer Author : ray
