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.