从VBA/Excel打开Goog​​le Chrome

Sam*_*Sam 16 excel vba google-chrome excel-vba

我正在尝试从VBA打开Chrome浏览器.我了解Chrome不支持ActiveX设置,所以我很好奇是否有任何解决方法?

Dim ie As Object 
Set ie = CreateObject("ChromeTab.ChromeFrame")
ie.Navigate "google.ca" 
ie.Visible = True
Run Code Online (Sandbox Code Playgroud)

ray*_*ray 24

shell("C:\Users\USERNAME\AppData\Local\Google\Chrome\Application\Chrome.exe -url http:google.ca")
Run Code Online (Sandbox Code Playgroud)

  • 嗨@ray,如果它是一个动态网站怎么办。我尝试了 Shell(chromePath 和“-url”和网站地址)。但它无法加载页面而只加载了谷歌的主页。 (2认同)
  • 我确定你的意思是 - 但是用户需要在此代码运行之前找出当前用户是谁.您可以使用:`current_user =(Environ $("Username"))`然后执行`ChromePath ="""c:\ users \"&current_user&"\ AppData\Local\Google\Chrome\Application\Chrome. EXE """` (2认同)

小智 7

也在这里工作:

Sub test544()

  Dim chromePath As String

  chromePath = """C:\Program Files\Google\Chrome\Application\chrome.exe"""

  Shell (chromePath & " -url http:google.ca")

End Sub
Run Code Online (Sandbox Code Playgroud)


小智 7

我找到了一种更简单的方法来做到这一点,即使您不知道镶边所在的路径,它也能完美地工作。

首先,您必须将此代码粘贴到模块的顶部。

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

Run Code Online (Sandbox Code Playgroud)

之后,您必须创建这两个模块:

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
Run Code Online (Sandbox Code Playgroud)

有了这个,您将能够(如果您愿意)为 url 设置一个变量,或者像硬编码一样保留它。

Ps:它适用于其他浏览器,只需将“Chrome.exe”更改为 opera、bing 等即可。