question

cpamac-4893 avatar image
0 Votes"
cpamac-4893 asked MiyukiIizuka-6345 commented

how to close microsoft edge browser coding by excel vba

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.

office-vba-devoffice-scripts-excel-dev
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

kinuasa avatar image
0 Votes"
kinuasa answered cpamac-4893 commented

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
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

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
Next

(FIn)

0 Votes 0 ·

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.

1 Vote 1 ·
TvanStiphout avatar image
0 Votes"
TvanStiphout answered Castorix31 commented

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

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

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 ))



0 Votes 0 ·
MiyukiIizuka-6345 avatar image
0 Votes"
MiyukiIizuka-6345 answered MiyukiIizuka-6345 commented

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




· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

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.

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

  1. 表示されているウィンドウからプロセスIDを取得

  2. 取得したプロセスIDからプロセス名を取得

  3. プロセス名が「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)等の手段を使用された方が良いだろうと思います。


1 Vote 1 ·

kinuasa-san

早速のコメントありがとうございます。(また、日本語で書いて下さり感謝です(^^)。)
更に、コードも作成して下さり大感謝です。
短期間にここまでして下さり、頭が下がります。
コードを一見しただけですが、こんなにボリュームのあるコードでないと指定のタブを閉じられないのですね。。。自力では到底無理でした。
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.



0 Votes 0 ·