使用 Chrome 或 MS Edge 启动浏览器

使用 Chrome 或 MS Edge 启动浏览器

我有 VBA 代码,用于从 excel 中的列单元格打开多个 URL。如何将浏览器更改为使用 Chrome,而不是使用 IE。

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim lrow As Long, lcol As Long
Dim linkcell As Range
Dim loopVar As Long
Dim IE As Object
   
Set IE = CreateObject("Internetexplorer.application")


lrow = Range("A1").End(xlDown).Row
    
If Not Intersect(Target, Cells(1, 2)) Is Nothing Then
    Cancel = True
  
    For Each linkcell In Range("E2:E" & lrow)

        With IE
            If loopVar = 0 Then
                .navigate linkcell
            Else
                .navigate linkcell, CLng(2048)
            End If
          
            Do While .readyState <> 4
              DoEvents
            Loop
          
            .Left = 0
            .Top = 0
            .Toolbar = 1
            .StatusBar = 1
            .Height = 600
            .Width = 900
            .resizable = True
            .Visible = True
          
        End With
        loopVar = loopVar + 1
      
     Next linkcell
    
     IE.navigate "https://www.costco.com", CLng(2048)
    
    ShowWindow IE.hWnd, SW_SHOWMAXIMIZED
            
    Set IE = Nothing
    
End Sub     
   

答案1

下面是一个使用 Chrome 的简化脚本。我没有测试任何东西,测试工作留给你。我列出了一些可以帮助你进一步开发脚本的来源。

剧本:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim lrow As Long, lcol As Long
Dim linkcell As Range
   
Dim path As String = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86)
Dim executable As String = Path.Combine(path, "Google\\Chrome\\Application\\chrome.exe")

lrow = Range("A1").End(xlDown).Row
    
If Not Intersect(Target, Cells(1, 2)) Is Nothing Then
    For Each linkcell In Range("E2:E" & lrow)
        Process.Start(executable, linkcell.Value)
     Next linkcell
End Sub

这里有一些将 Chrome 窗口移到前台的有用资源(尽管这可能不是绝对必要的):

答案2

将代码放入我的文件时出现错误。

在此处输入图片描述

相关内容