【问题标题】:Texbox to setup google chrome path?Texbox 设置谷歌浏览器路径?
【发布时间】:2019-12-03 03:51:43
【问题描述】:

我有一个命令按钮,点击后会打开谷歌。 这是我正在使用的代码。

    Private Sub CommandButton1_Click()
    Dim chromePath As String
    chromePath = """C:\Program Files\Google\Chrome\Application\chrome.exe"""
      Shell (chromePath & " -url https://google.com"), vbNormalFocus
      Unload Me
    End Sub

此工作簿与其他人共享,但在他们的 PC 上不起作用,因为其中一些人有不同的 chrome 路径,也不知道如何转到 VB 编辑器进行必要的更改。

我的问题: 是否可以在用户表单的文本框中输入 chromepath,然后在 VBE 中进行更改而无需转到 VBE?

【问题讨论】:

  • 如果你只是打开一个网页,你不需要知道Chrome安装在哪里。
  • 这条评论根本没有帮助。
  • 好的,那我来回答你的问题。是的,可以在路径中使用文本框,但这不是最好的解决方案,因为用户也不会知道路径。如果您正在尝试这样做,它不会为您修改代码。
  • 如果这个人没有安装 Chrome 怎么办?
  • 他们都知道如何找到chrome的路径,右键单击桌面上的chrome应用程序或快捷方式链接,检查目标。然后复制目标路径,将其粘贴到文本框中,这应该会更改 VBA 中的代码。但既然你提到它不会改变代码,你已经回答了我的问题。

标签: excel vba


【解决方案1】:

如果意图只是以 google 作为 URL 启动 chrome,那么您可以尝试以下代码。只要安装了 chrome,它就可以在所有计算机上运行(因为 chrome.exe 不需要路径)。

使用 WScript.Shell:

Dim oShell As Object
Set oShell = CreateObject("WScript.Shell")
Dim strCmd As String
strCmd = "cmd /k start chrome.exe -url https://google.com"
oShell.Exec strCmd
Set oShell = Nothing

使用普通 Shell

Dim strCmd As String
strCmd = "cmd /k start chrome.exe -url https://google.com"
Shell strCmd, vbHide

【讨论】:

  • 哇哦,刚试过。这甚至更好。你解决了我的问题。 :-)
  • 我用来测试的电脑是 64 位的,chrome 是 64 位的,所以我会说是的!
【解决方案2】:

如何使用单元格来保持用户对 chrome.exe 路径的偏好。然后您可以使用单元格值而不是硬编码的值。

Dim chromePath As String
chromePath = ThisWorkbook.Names("ChromePath").RefersToRange.Value 
If chromePath = "" Then
    chromePath = "C:\Program Files\Google\Chrome\Application\chrome.exe"
End If
Shell ("""" & chromePath & """ -url https://google.com"), vbNormalFocus

【讨论】:

  • 这听起来是个不错的选择。一旦我在办公室,我会稍后尝试。 :-)
猜你喜欢
  • 2011-04-26
  • 1970-01-01
  • 2017-06-22
  • 2013-09-28
  • 1970-01-01
  • 2011-08-16
  • 1970-01-01
  • 2014-11-29
  • 1970-01-01
相关资源
最近更新 更多