0

I have a commandbutton, when clicked, it opens google. Here is the code I am using.

    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

This workbook is shared with other people however it doesn't work on their PC as some of them have a different chrome path and also no idea how to go to the VB editor to make the necessary changes.

My question: Would it be possible to enter the chromepath in a textbox in a userform which then makes the changes in VBE without going to VBE?

  • If you are just opening a web page, you dont need to know where Chrome is installed. – braX Dec 03 '19 at 04:04
  • This comment didn't help at all. –  Dec 03 '19 at 04:05
  • OK, then let me answer your question. Yes, it would be possible to use the text box in the path, it's not the best solution tho, as the user is not going to know the path either. And it's not going to modify the code for you if that's what you are trying to do. – braX Dec 03 '19 at 04:11
  • And what if the person doesn't have Chrome installed? – braX Dec 03 '19 at 04:17
  • They all know how to find out the path to chrome, right click on the chrome application or shortcut link on the desktop, check the target. Then copy the target path, paste it in the textbox and this should change the code in VBA. But since you mentioned that it won't change the code, you already answered my question. –  Dec 03 '19 at 04:30
  • OK, then change the code to use the textbox's text property instead of what you currently have hard-coded. It's just a string like any other string, so concatenate it using `&` like you already know how to do. Just know that they are going to have to paste that chrome path into the text box every time they run it, which seems very cumbersome. – braX Dec 03 '19 at 04:32
  • Hmm, that's really cumbersome. What if I create a save button? Would that save it to the code permanently? Like a setting page, you know what I mean? –  Dec 03 '19 at 04:34
  • Nothing is going to modify the code for you, dear. you would have to use a variable and set the variable somehow. You could try this tho: https://stackoverflow.com/questions/3166265/open-an-html-page-in-default-browser-with-vba – braX Dec 03 '19 at 04:40

2 Answers2

1

If intent is to just start chrome with google as URL then you can try below code. It should work on all computers (as there's no path required for chrome.exe) provided they have chrome installed.

Use 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

Use normal Shell

Dim strCmd As String
strCmd = "cmd /k start chrome.exe -url https://google.com"
Shell strCmd, vbHide
shrivallabha.redij
  • 5,832
  • 1
  • 12
  • 27
0

How about using a cell to keep the user preference of chrome.exe path. Then you can use the cell value instead of the hard coded one.

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
Kosuke Sakai
  • 2,336
  • 2
  • 5
  • 12
  • That sounds like a great option. Will try it out later once I am in the office. :-) –  Dec 03 '19 at 04:48