从VBA / Excel打开Goog​​le Chrome

时间:2011-05-06 18:08:37

标签: vba google-chrome excel-vba excel

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

Dim ie As Object 
Set ie = CreateObject("ChromeTab.ChromeFrame")
ie.Navigate "google.ca" 
ie.Visible = True

6 个答案:

答案 0 :(得分:19)

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

答案 1 :(得分:7)

也在这里工作:

Sub test544()

  Dim chromePath As String

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

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

End Sub

答案 2 :(得分:4)

我找到了一种更简单的方法,即使您不知道Chrome所在的路径,它也可以完美地工作。

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

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ads_category">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/myAdsRecycleView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:background="@color/colorGray"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

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

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

有了这个,您将能够(如果需要)为url设置一个变量或将其保留为硬编码。

Ps:它仅将“ Chrome.exe”更改为Opera,Bing等,对于其他浏览器而言效果最佳。

答案 3 :(得分:2)

您可以使用以下vba代码并将其输入到Excel中的标准模块中。可以输入网站列表,并且应该在Excel的单元格A1中输入这样的网站 - www.stackoverflow.com

ActiveSheet.Cells(1,2).Value仅获取Excel中单元格B1上的网站链接数量,并根据您在工作表上放置的网站链接数量反复循环代码。因此,Chrome会为每个网站链接打开一个新标签。

我希望这对您拥有的动态网站有所帮助。

Sub multiplechrome()

    Dim WebUrl As String
    Dim i As Integer

    For i = 1 To ActiveSheet.Cells(1, 2).Value
        WebUrl = "http://" & Cells(i, 1).Value & """"
        Shell ("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe -url " & WebUrl)

    Next
End Sub

答案 4 :(得分:0)

上面@ray给出的答案非常有效,但是请确保使用正确的路径打开文件。如果右键单击图标并单击属性,则应该看到实际路径在哪里,只需复制过去即可,它应该可以工作。

Properties

答案 5 :(得分:0)

您可以使用 selenium basic 启动 Chrome 并与之交互。安装后,您需要添加对 Selenium 类型库的引用。

Option Explicit
Public Sub Demo()
    Dim d As WebDriver
    Set d = New ChromeDriver
    Const URL = "https://www.google.com/"

    With d
        .Start "Chrome"
        .get URL
        .FindElementById("lst-ib").SendKeys "Selenium basic GitHub"
         .FindElementsByTag("form")(1).FindElementByCss("input[value='Google Search']").Click
        '.Quit
    End With
End Sub