none
VBA - Open Browser Window RRS feed

  • Question

  • I use Shell command for open a window of chrome browser when the user click on a button in outlook. But, for each click, there is a new chrome window. Is it possible to use the same chrome window for each click ?

    Option Explicit
    
    Private Declare Function ShellExecute _
      Lib "shell32.dll" Alias "ShellExecuteA" ( _
      ByVal hWnd As Long, _
      ByVal Operation As String, _
      ByVal Filename As String, _
      Optional ByVal Parameters As String, _
      Optional ByVal Directory As String, _
      Optional ByVal WindowStyle As Long = vbMinimizedFocus _
      ) As Long
    
    Public Const BrowserLocation = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
    Public Const msgUrl = "http://google.com/"
    Public oShell As Object
    
    
    Sub OpenBrowser(params As String, Optional sAction As String)
        Dim sUrl As String
        sUrl = msgUrl & params
    
        Dim result As Long
        result = ShellExecute(0, "Open", sUrl, "", "", 0)
    
    
    End Sub


    Thank you for your help

    Monday, October 28, 2013 11:14 AM

Answers

  • Yannmc,

    As Google Chrome do not expose its object model to VBA, I suggest you to use Internet Explorer instead. You can refer to the IE Application as an object, and so you have methods to open a new tab, navigate through a different URL and so on.

    The only tricky part is the code to VBA get an already opened instance of IE, since you can't use GetObject function to return it. But I created a function so you can just use a one line code to get (or create) an Internet Explorer instance.

    See the code below:

    Sub fncMain()
        Dim appIE As Object 'InternetExplorer
        
        Set appIE = fncGetIE
        appIE.Navigate "http://www.ambienteoffice.com.br"
    End Sub
    
    Function fncGetIE() As Object 'InternetExplorer
        Dim appShell As Variant
        Dim varWindows As Variant
        Dim var As Variant
        Dim Temp As Object 'InternetExplorer
            
        Set appShell = CreateObject("Shell.Application")
        Set varWindows = appShell.Windows()
        
        On Error Resume Next
        For var = 0 To varWindows.Count - 1
            If InStr(LCase(varWindows.Item(var).FullName), "iexplore.exe") > 0 Then
                Set Temp = varWindows.Item(var)
                Exit For
            End If
        Next var
        On Error GoTo 0
        
        If Temp Is Nothing Then
            Set Temp = CreateObject("InternetExplorer.Application")
        End If
        Temp.Visible = True
        
        Set fncGetIE = Temp
    End Function


    Felipe Costa Gualberto - http://www.ambienteoffice.com.br


    • Edited by Felipe Costa GualbertoMVP Tuesday, October 29, 2013 8:53 PM Mispelling
    • Marked as answer by Yannmc Friday, November 8, 2013 9:18 AM
    • Unmarked as answer by Yannmc Friday, November 8, 2013 9:19 AM
    • Marked as answer by Yannmc Friday, November 8, 2013 9:19 AM
    Tuesday, October 29, 2013 9:17 AM

All replies

  • Hi,

    Thank you for posting in the MSDN Forum.

    Since the issue is more relate to the using of VBA macro, I'd like to move it to VBA forum.

    The reason why we recommend posting appropriately is you will get the most qualified pool of respondents, and other partners who read the forums regularly can either share their knowledge or learn from your interaction with us.

    Thanks for your understanding.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, October 29, 2013 6:45 AM
    Moderator
  • Hi,

    According to your description, I think you want to open a web page in a new tab instead of opening a new browser window.

    You could try the code below.

    Shell ("<C:\Program Files (x86)\Google\Chrome\Application\chrome.exe -url http://www.google.com>")

    Since your code works well in my IE browser if I don't set the BrowserLocation, I think your problem is more related to chrome browser. If you have more requirement, I suggest you to post to the Chrome forum for more efficient response.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, October 29, 2013 6:58 AM
    Moderator
  • Yannmc,

    As Google Chrome do not expose its object model to VBA, I suggest you to use Internet Explorer instead. You can refer to the IE Application as an object, and so you have methods to open a new tab, navigate through a different URL and so on.

    The only tricky part is the code to VBA get an already opened instance of IE, since you can't use GetObject function to return it. But I created a function so you can just use a one line code to get (or create) an Internet Explorer instance.

    See the code below:

    Sub fncMain()
        Dim appIE As Object 'InternetExplorer
        
        Set appIE = fncGetIE
        appIE.Navigate "http://www.ambienteoffice.com.br"
    End Sub
    
    Function fncGetIE() As Object 'InternetExplorer
        Dim appShell As Variant
        Dim varWindows As Variant
        Dim var As Variant
        Dim Temp As Object 'InternetExplorer
            
        Set appShell = CreateObject("Shell.Application")
        Set varWindows = appShell.Windows()
        
        On Error Resume Next
        For var = 0 To varWindows.Count - 1
            If InStr(LCase(varWindows.Item(var).FullName), "iexplore.exe") > 0 Then
                Set Temp = varWindows.Item(var)
                Exit For
            End If
        Next var
        On Error GoTo 0
        
        If Temp Is Nothing Then
            Set Temp = CreateObject("InternetExplorer.Application")
        End If
        Temp.Visible = True
        
        Set fncGetIE = Temp
    End Function


    Felipe Costa Gualberto - http://www.ambienteoffice.com.br


    • Edited by Felipe Costa GualbertoMVP Tuesday, October 29, 2013 8:53 PM Mispelling
    • Marked as answer by Yannmc Friday, November 8, 2013 9:18 AM
    • Unmarked as answer by Yannmc Friday, November 8, 2013 9:19 AM
    • Marked as answer by Yannmc Friday, November 8, 2013 9:19 AM
    Tuesday, October 29, 2013 9:17 AM
  • Thank you very much for your help.

    I used CreateObject("InternetExplorer.Application"), it's working.

    Regards

    Friday, November 8, 2013 9:21 AM