【问题标题】:How to open hyperlinks based on selection with chrome in Excel如何在 Excel 中使用 chrome 打开基于选择的超链接
【发布时间】:2016-06-15 04:11:07
【问题描述】:

我知道你可以做到

Sub Chrome()

Dim chromePath As String
chromePath = """C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"""
Shell (chromePath & " -url https://www.google.com")
Shell (chromePath & " -url path")
Shell (chromePath & " -url path")
Shell (chromePath & " -url path")
Shell (chromePath & " -url path")

End Sub

但如果我事先不知道超链接或 URL'¿s,我就无法在代码中真正写出它们,我确实有带有超链接的单元格,我正在努力实现能够选择 X 数量的单元格并在 Chrome 的不同选项卡中打开超链接。

我尝试了一些类似于

的愚蠢方法
Sub Open_HyperLinks()

Dim chromePath As String
chromePath = """C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"""
    Dim hl As Hyperlink
    On Error Resume Next
    For Each hl In Selection.Hyperlinks
        Shell (chromePath & " -url hl")
    Next hl
End Sub

当然没用……

【问题讨论】:

    标签: vba excel google-chrome hyperlink


    【解决方案1】:

    您应该使用Hyperlinks 集合中每个超链接 的.Address 属性。

    Sub Open_HyperLinks()
        Dim chromePath As String, hl As Hyperlink
    
        chromePath = Environ("PROGRAMFILES(X86)") & "\Google\Chrome\Application\chrome.exe"
        'On Error Resume Next
        For Each hl In Selection.Hyperlinks
            Shell chromePath & " -url " & hl.Address
        Next hl
    End Sub
    

    我从头开始解决了您的 %PROGRAMFILES(X86)% 环境变量,但是硬编码的驱动器和路径应该也能正常工作。

    【讨论】:

    • ...有没有办法强制打开标签之间的时间出于某种原因打开标签以随机顺序打开时...我需要它们从上到下打开.. .
    • 您可以尝试将 -newtab 参数添加到命令行(如另一个答案中所建议的那样),但我没有太多从 VBA 操作 Chrome 的经验,只能从 shell 启动应用程序。有几种方法可以让您等待预定的秒数,但除非是 IE,否则我认为您不能等到 Chrome 完成加载 URL 而不从操作系统的有利位置确定处理器或网络活动。
    • 所以让我们等一下,我们想等 5 秒钟,我应该使用 wait timeout 还是两者兼而有之?
    • dim then as double: then =now+timerserial(0, 0, 5): do while now<then: doevents: loop
    【解决方案2】:

    Shell() 方法将所有参数作为字符串传递到命令行。确保“-url hl”中的 hl 对象在引号之外。否则 Shell() 将“hl”作为超链接传递,而不是“https://www.google.com”。

    出于同样的原因,去掉 chromePath 周围的多余引号。

    试试这个:

    Sub Open_HyperLinks()
    
        Dim chromePath As String
        chromePath = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
            Dim hl As Hyperlink
            On Error Resume Next
            For Each hl In Selection.Hyperlinks
                Shell (chromePath & " -url -newtab " & hl)
            Next hl
    End Sub
    

    注意:我还没有为 chrome 可执行文件测试过这些标志。

    【讨论】:

    • 尝试在 Shell() 调用中将“hl”替换为“hl.Address”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-29
    • 1970-01-01
    • 1970-01-01
    • 2021-07-29
    • 1970-01-01
    • 2016-04-22
    • 2021-09-11
    相关资源
    最近更新 更多