none
Need help with VBA code for Windows 10 Microsft edge browser RRS feed

  • Question

  • We have automated xl template written in macros and VBA to automate tasks for one of our web application. When the script runs it opens up IE and fill all the details and submit the form. The issue now is windows 10 does have Microsoft edge and VBA is not compatible with edge as it does with IE. So need to know how to open edge through VBA or how to handle edge using VBA.
    Tuesday, January 29, 2019 10:59 AM

All replies

  • There is a program called SeleniumBasic.  It is capable of controlling Edge using the stand-alone webdriver.  Starting with Windows 10 1809 it doesn't offer a stand-alone webdriver.  It delivers webdriver with Windows 10 in System32 directory.  SeleniumBasic doesn't work with it.  I also have a C# program controlling Edge and I haven't been able to get it to work with the new driver and I can't find any examples on how to get it to work.

    I'm using SeleniumBasic with Chrome.  You are out of luck.


    • Edited by mogulman52 Tuesday, January 29, 2019 11:38 PM
    Tuesday, January 29, 2019 11:36 PM
  • Hi Angari,

    In order to open a specific web page on Microsoft Edge, do like this:
    VBA code:
    CreateObject("Shell.Application").ShellExecute "microsoft-edge:http://hokusosha.com"
    
    Please change "http://hokusosha.com" as you like.

    Sorry, I could not find how to launch Microsoft Edge without web page.

    Regards,

    Ashidacchi -- https://ssl01.rocketnet.jp/hokusosha.com/default.html

    Wednesday, January 30, 2019 12:51 AM
  • Hi Ashidacchi,

    how to login an facebook account with Microsoft Edge browser via VBA ?

    we can do this or not ?

    Best regards !

    Friday, May 31, 2019 4:40 PM
  • Hi Thong Hoang Tien,

    Write the below code:
    CreateObject("Shell.Application").ShellExecute "microsoft-edge:https://www.facebook.com/"

    cf. Please add a button (ActiveX control) named "CommandButton1" on a work sheet,
         
      and write the below in the button.
         

    Regards,

    Ashidacchi -- https://ssl01.rocketnet.jp/hokusosha.com/default.html

    Saturday, June 1, 2019 6:11 AM
  • I think he asked if you can login to facebook.  Your solution only opens facebook but doesn't login.  The only way to control Edge browser is through Selenium.  You can use the Win10 built-in webdriver.   There is no direct way to use it in VBA.  C# supports it.   I think you could write a C# .COM DLL to access it from VBA.   This is a very complicated solution.
    Saturday, June 1, 2019 11:35 AM
  • Hi mongulman52,

    I made a test for my code and it could open Edge browser with my login page. 

    I think this can be done after login my Facebook page with Edge and save password  manually.

    To Thong Hoang Tien:
    Please try to 
    (1) logon your Face book page and save your password
    (2) run VBA code 

    Regards,

    Ashidacchi -- https://ssl01.rocketnet.jp/hokusosha.com/default.html


    • Edited by Ashidacchi Saturday, June 1, 2019 9:06 PM
    Saturday, June 1, 2019 9:05 PM
  • Hi,
    I recommend you try 'SeleniumBasic'.
    ('--jwp' option is necessary for new version of EdgeDriver.)

    Option Explicit
    
    Public Sub Sample()
      Dim pid As Long
      Dim pno As Long: pno = 17556
      
      pid = StartEdgeDriver(PortNo:=pno)
      If pid = 0 Then Exit Sub
      With CreateObject("Selenium.WebDriver")
        .StartRemotely "http://localhost:" & pno & "/", "MicrosoftEdge"
        .Get "https://www.bing.com/"
        .FindElementById("sb_form_q").SendKeys "abcdefg"
        MsgBox "pause", vbInformation + vbSystemModal
        .Quit
      End With
      TerminateEdgeDriver pid
      MsgBox "done.", vbInformation + vbSystemModal
    End Sub
    
    Private Function StartEdgeDriver( _
      Optional ByVal DriverPath As String = "C:\Windows\System32\MicrosoftWebDriver.exe", _
      Optional ByVal PortNo As Long = 17556) As Long
      
      Dim DriverFolderPath As String
      Dim DriverName As String
      Dim Options As String
      Dim itm As Object, itms As Object
      Dim pid As Long: pid = 0
      
      With CreateObject("Scripting.FileSystemObject")
        If .FileExists(DriverPath) = False Then GoTo Fin
        DriverFolderPath = .GetParentFolderName(DriverPath)
        DriverName = .GetFileName(DriverPath)
      End With
      
      'check already running process
      Set itms = CreateObject("WbemScripting.SWbemLocator").ConnectServer.ExecQuery _
                 ("Select * From Win32_Process Where Name = '" & DriverName & "'")
      If itms.Count > 0 Then
        For Each itm In itms
          pid = itm.ProcessId: GoTo Fin
        Next
      End If
      
      'execute WebDriver
      Options = " --host=localhost --jwp --port=" & PortNo
      With CreateObject("WbemScripting.SWbemLocator").ConnectServer.Get("Win32_Process")
        .Create DriverPath & Options, DriverFolderPath, Null, pid
      End With
      
    Fin:
      StartEdgeDriver = pid
    End Function
    
    Private Sub TerminateEdgeDriver(ByVal ProcessId As Long)
      Dim itm As Object, itms As Object
       
      Set itms = CreateObject("WbemScripting.SWbemLocator").ConnectServer.ExecQuery _
                 ("Select * From Win32_Process Where ProcessId = " & ProcessId & "")
      If itms.Count > 0 Then
        For Each itm In itms
          itm.Terminate: Exit For
        Next
      End If
    End Sub

    I confirmed this code in the following environment.

    1. Windows 10 Pro ver.1903 x64
    2. Excel ver.1906 x86
    3. Microsoft Edge 44.18362.1.0
    4. SeleniumBasic v2.0.9.0

    Best regards,
    kinuasa

    Monday, June 3, 2019 4:14 AM
  • Another way is to get the HTMLDocument object from the window of 'Internet Explorer_Server' class.
    (ref. http://www.mvps.org/emorcillo/en/code/vb6/iedom.shtml)
    but I do not recommend it.

    'code for x86 module.
    Option Explicit
    
    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWndParent As Long, ByVal hWndChildAfter As Long, ByVal lpszClass As String, ByVal lpszWindow As String) As Long
    Private Declare Function IIDFromString Lib "ole32" (lpsz As Any, lpiid As Any) As Long
    Private Declare Function ObjectFromLresult Lib "oleacc" (ByVal lResult As Long, riid As Any, ByVal wParam As Long, ppvObject As Object) As Long
    Private Declare Function RegisterWindowMessage Lib "user32" Alias "RegisterWindowMessageA" (ByVal lpString As String) As Long
    Private Declare Function SendMessageTimeout Lib "user32" Alias "SendMessageTimeoutA" (ByVal hWnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long, ByVal fuFlags As Long, ByVal uTimeout As Long, lpdwResult As Long) As Long
    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    
    Public Sub Sample()
      Dim hEdge As Long
      Dim hIES As Long
      Dim d As Object
      
      CreateObject("Shell.Application").ShellExecute "microsoft-edge:http://www.bing.com/"
      Sleep 2000
      
      hEdge = FindWindowEx(0, 0, "TabWindowClass", vbNullString)
      If hEdge = 0 Then Exit Sub
      hIES = FindWindowEx(hEdge, 0, "Internet Explorer_Server", vbNullString)
      If hIES = 0 Then Exit Sub
      Set d = GetHTMLDocumentFromIES(hIES)
      If d Is Nothing Then Exit Sub
      d.getElementById("sb_form_q").Value = "Microsoft Office"
    End Sub
    
    Private Function GetHTMLDocumentFromIES(ByVal hWnd As Long) As Object
      Dim msg As Long, res As Long
      Dim iid(0 To 3) As Long
      Dim ret As Object, obj As Object
      Const SMTO_ABORTIFHUNG = &H2
      Const IID_IHTMLDocument2 = "{332C4425-26CB-11D0-B483-00C04FD90119}"
      
      msg = RegisterWindowMessage("WM_HTML_GETOBJECT")
      SendMessageTimeout hWnd, msg, 0, 0, SMTO_ABORTIFHUNG, 1000, res
      If res Then
        IIDFromString StrPtr(IID_IHTMLDocument2), iid(0)
        If ObjectFromLresult(res, iid(0), 0, obj) = 0 Then Set ret = obj
      End If
      Set GetHTMLDocumentFromIES = ret
    End Function

    regards,
    kinuasa
    Monday, June 3, 2019 4:40 AM
  • I want to thank きぬあさ for the SeleniumBasic solution.  Works great.

    Sunday, June 9, 2019 1:02 PM