how to close microsoft edge browser coding by excel vba

cpa mac 26 Reputation points
2021-09-25T07:20:57.067+00:00

Anybody kindly please let me know how to close microsoft edge browser coding by excel vba.

Before, Ashidacchi-san posting the code example how to launch microsoft edge browser,

CreateObject("Shell.Application").ShellExecute "microsoft-edge:http://hokusosha.com".

I would like to know the adverse, close way using excel vba.

Thank you.

Microsoft 365 and Office | Development | Other
Developer technologies | Visual Basic for Applications
0 comments No comments

Answer accepted by question author
kinuasa 371 Reputation points
2021-09-27T02:27:11.19+00:00

You can also use UI Automation to close Edge window.

'* Add a Reference to the UIAutomationClient Library(UIAutomationCore.dll)
Public Sub CloseEdgeWindow()
  Dim uiAuto As CUIAutomation
  Set uiAuto = New CUIAutomation
  Dim elmRoot As IUIAutomationElement
  Set elmRoot = uiAuto.GetRootElement
  Dim cndChromeWidgetWindows As IUIAutomationCondition
  Set cndChromeWidgetWindows = uiAuto.CreatePropertyCondition( _
        UIA_ClassNamePropertyId, _
        "Chrome_WidgetWin_1" _
      )
  Dim aryChromeWidgetWindows As IUIAutomationElementArray
  Set aryChromeWidgetWindows = elmRoot.FindAll(TreeScope_Children, cndChromeWidgetWindows)
  Dim wptn As IUIAutomationWindowPattern, i As Integer
  For i = 0 To aryChromeWidgetWindows.Length - 1
    If aryChromeWidgetWindows.GetElement(i).CurrentName Like "*- Microsoft" & ChrW(&H200B) & " Edge" Then
      Set wptn = aryChromeWidgetWindows.GetElement(i).GetCurrentPattern(UIA_WindowPatternId)
      wptn.Close
    End If
  Next
End Sub

Was this answer helpful?


2 additional answers

Sort by: Most helpful
  1. Miyuki Iizuka 1 Reputation point
    2021-10-11T03:58:14.273+00:00

    kinuasa-san

    Your advise was great. So I make use of your code (cf: following).
    But I have 2 questions.

    1. When Edge doesn't have any tabs, "aryChromeWidgetWindows.Length" seems not to detect Edge so that "aryChromeWidgetWindows.GetElement(i).CurrentName" can't get "Edge's caption".
      As a result, Edge isn't closed.
    2. If Edge has some tabs, how to close the designated web site.

    〔Due to my company's policy, I'd like to solve this matter without installing other software like "Selenium Basic".〕

    I would be happy if you could post advise more.

    (↓↓Cf. I'm making code.↓↓)

    Sub test()

    Dim BrWsr As Variant

    Dim uiAuto As CUIAutomation
    Set uiAuto = New CUIAutomation

    Dim elmRoot As IUIAutomationElement
    Set elmRoot = uiAuto.GetRootElement

    Dim cndChromeWidgetWindows As IUIAutomationCondition
    Set cndChromeWidgetWindows = uiAuto.CreatePropertyCondition( _
    UIA_ClassNamePropertyId, _
    "Chrome_WidgetWin_1" _
    )
    Dim aryChromeWidgetWindows As IUIAutomationElementArray
    Set aryChromeWidgetWindows = elmRoot.FindAll(TreeScope_Children, cndChromeWidgetWindows)

    Dim wptn As IUIAutomationWindowPattern, i As Integer

        Set BrWsr = CreateObject("Shell.Application")
    
        TEST_site = "https://www.yahoo.co.jp/"
    
        Url = "microsoft-edge:" & TEST_site
    
        BrWsr.ShellExecute Url
    
            For i = 0 To aryChromeWidgetWindows.Length - 1
                 If aryChromeWidgetWindows.GetElement(i).CurrentName Like "*- Microsoft" & ChrW(&H200B) & " Edge" Then
                   Set wptn = aryChromeWidgetWindows.GetElement(i).GetCurrentPattern(UIA_WindowPatternId)
                   wptn.Close
                 End If
            Next
    

    End Sub

    Was this answer helpful?


  2. Tom van Stiphout 40,216 Reputation points MVP Volunteer Moderator
    2021-09-25T21:57:49.94+00:00

    Here are a few hints:
    You can get the Window handle of an application using FindWindow API. You may want to use WinSpy or Spy++ to find out what the window class name is.
    You can close a window by using PostMessage API to send it a WM_CLOSE message.

    With the above keywords you should be able to do your own research and put the code together.
    For the API declarations this is a good reference: https://www.microsoft.com/en-us/download/details.aspx?id=9970

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.