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.

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
End Sub
											

So happy to let me know. This code is what I want to know and surely works. Great and thanks so much! I am going to follow the coding and apply other cases. Thanks again, Kinuasa-san.

If you could imagine how to translate VBA for IE coding below into VBA for Edge, please give me hints. I still cannot find the key vba code which is for IE and which is for Edge.

(Start)

Dim objIE As Object
Dim sh As Object
Dim win As Object
Dim document_title As String

Set sh = CreateObject("Shell.Application")
For Each win In sh.Windows
On Error Resume Next
document_title = ""
document_title = win.document.Title
On Error GoTo 0

If InStr(document_title, "murai") > 0 Then
Set objIE = win
Exit For

End If

(FIn)

Microsoft Edge is not support the COM automation like IE.
For automation scenarios, you can use the WebDriver.

  • https://learn.microsoft.com/en-us/microsoft-edge/webdriver-chromium/
  • https://stackoverflow.com/questions/69118936/vba-script-to-convert-from-internet-explorer-to-edge-or-chrome-browser
  • Yes, as you mentioned, the WebDriver like Selenium Basic could be fine if install is allowed. Many PC controlled under company unfortunately are not around me, at least though.
    So, I am looking for VBA that directly codes on Edge or Chrome browser.
    The translation tool from for IE into Edge or Chrome would be great but still I cannot find it.

    kinuasa-san's code to close edge window is great for me. Thank to it, a VBA I used via IE was able to convert them via Edge smoothly. I don't imagine that just one line code is fine in IE. But still just only one VBA I ever code is converted.

    Anyway thanks your hints, thanks your time, thanks your advise.

    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

    Yes, PostMessage + WM_CLOSE is the right method (I tested on my version, Edge just doesn't restore previously opened tabs when it is re-started)
    The only problem is that the class is "Chrome_WidgetWin_1" and other apps can have this class, at least "Google Chrome"
    So he should use APIs like EnumWindows to check the class name then get the .exe from the PID (from GetWindowThreadProcessId ))

    kinuasa-san

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

  • 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.
  • 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
    

    End Sub

    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.

    タブが無い状態というのが分からないのですが、一つの方法として、

  • 表示されているウィンドウからプロセスIDを取得
  • 取得したプロセスIDからプロセス名を取得
  • プロセス名が「msedge.exe」である場合に処理を行う
  • といった方法が考えられます。

    If Edge has some tabs, how to close the designated web site.

    ページのタイトルを指定してEdgeのタブを閉じる方法が考えられます。
    プロセスIDからEdgeのウィンドウを判別して処理する方法とまとめると下記のようなコードです。

    https://gist.github.com/kinuasa/c28b76bc60122b1a2cd2ba63c5459796

    なお、私が上で挙げたコードはUIに依存するものです。
    バージョンアップに伴いEdgeのUI、ウィンドウ構造が変更された場合は動作しなくなるような、安定性に欠けるコードになりますので、プログラムによってEdgeを操作する場合は本来的には公式に用意されているWebDriver(Selenium)等の手段を使用された方が良いだろうと思います。

    更に、コードも作成して下さり大感謝です。
    短期間にここまでして下さり、頭が下がります。
    コードを一見しただけですが、こんなにボリュームのあるコードでないと指定のタブを閉じられないのですね。。。自力では到底無理でした。
    kinuasa様のアドバイスを元に取り組んでみようと思います。

    Thanks for your advise so much.
    Furthermore you made a code which solved my question for short time. I admire you.
    I'll try to solve these matters based on your advise again.