Office関連

アクティブなIEのタブを閉じるVBAマクロ

VBA アクティブ IE タブ 閉じる」といったキーワード検索でのアクセスがありました。
キーワード通り、アクティブなInternet Explorerのタブをマクロから閉じる処理を実装したい方による検索だと思います。

このマクロがどの位有用なのかは分かりませんが、ちょっと興味があったので書いてみました。

Public Sub CloseCurrentIeTab1()
'Internet Explorerのアクティブなタブを閉じる
  Dim o As Object
  
  For Each o In GetObject("new:{9BA05972-F6A8-11CF-A442-00A0C90A8F39}") 'ShellWindows
    If LCase(TypeName(o)) = "iwebbrowser2" Then
      If LCase(TypeName(o.Document)) = "htmldocument" Then
        'StatusTextの変更でアクティブなタブを判断
        'http://scripting.cocolog-nifty.com/blog/2009/08/ie7ie8ie-ee95.html 参照
        o.StatusText = "dummy_string"
        If o.StatusText = "dummy_string" Then
          'o.ExecWB 45, 0 'OLECMDID_CLOSEでもいけそう
          o.Document.parentWindow.Open("about:blank", "_self").Close
          Exit For
        End If
      End If
    End If
  Next
End Sub

コード中にも書いている通り、StatusTextの変更でアクティブなタブを判断し(「IE7/IE8で、現在または最後にアクティブなIEを捕捉する」参照)、タブを閉じる処理を実装しています。

ただ、私が試したところでは連続で実行すると上手く閉じられない場合があり、動作が若干不安定でしたので、いつものUI Automationを使った処理も考えてみました。

Public Sub CloseCurrentIeTab2()
'Internet Explorerのアクティブなタブを閉じる
'※UIAutomationClient(UIAutomationCore.dll)要参照
  Dim ie As Object
  Dim uiAuto As CUIAutomation
  Dim elmIe As IUIAutomationElement
  Dim elmTab As IUIAutomationElement
  Dim elmCloseBtn As IUIAutomationElement
  Dim elmTabs As IUIAutomationElement
  Dim aryTabs As IUIAutomationElementArray
  Dim cnd As IUIAutomationCondition
  Dim iptn As IUIAutomationInvokePattern
  Dim i As Long
  
  Set ie = GetActiveIE
  If ie Is Nothing Then Exit Sub
  Set uiAuto = New CUIAutomation
  Set elmIe = uiAuto.ElementFromHandle(ByVal CLng(ie.Hwnd))
  If elmIe Is Nothing Then Exit Sub
  Set elmTabs = GetElement(uiAuto, _
                           elmIe, _
                           UIA_NamePropertyId, _
                           "タブ行", _
                           UIA_TabControlTypeId)
  If elmTabs Is Nothing Then Exit Sub
  Set cnd = uiAuto.CreatePropertyCondition( _
              UIA_ControlTypePropertyId, _
              UIA_TabItemControlTypeId _
            )
  Set aryTabs = elmTabs.FindAll(TreeScope_Subtree, cnd)
  For i = 0 To aryTabs.Length - 1
    Set elmTab = aryTabs.GetElement(i)
    If elmTab.GetCurrentPropertyValue(UIA_SelectionItemIsSelectedPropertyId) Then
      Set elmCloseBtn = GetElement(uiAuto, elmTab, UIA_ControlTypePropertyId, UIA_ButtonControlTypeId)
      If elmCloseBtn Is Nothing Then Exit Sub
      Set iptn = elmCloseBtn.GetCurrentPattern(UIA_InvokePatternId)
      iptn.Invoke
    End If
  Next
End Sub

Private Function GetActiveIE() As Object
'起動中のIE取得
  Dim o As Object
   
  For Each o In GetObject("new:{9BA05972-F6A8-11CF-A442-00A0C90A8F39}") 'ShellWindows
    If LCase(TypeName(o)) = "iwebbrowser2" Then
      If LCase(TypeName(o.Document)) = "htmldocument" Then
        Set GetActiveIE = o
        Exit For
      End If
    End If
  Next
End Function

Private Function GetElement(ByVal uiAuto As CUIAutomation, _
                            ByVal elmParent As IUIAutomationElement, _
                            ByVal propertyId As Long, _
                            ByVal propertyValue As Variant, _
                            Optional ByVal ctrlType As Long = 0) As IUIAutomationElement
  Dim cndFirst As IUIAutomationCondition
  Dim cndSecond As IUIAutomationCondition
    
  Set cndFirst = uiAuto.CreatePropertyCondition( _
                   propertyId, _
                   propertyValue _
                 )
  If ctrlType <> 0 Then
    Set cndSecond = uiAuto.CreatePropertyCondition( _
                      UIA_ControlTypePropertyId, _
                      ctrlType _
                    )
    Set cndFirst = uiAuto.CreateAndCondition( _
                     cndFirst, _
                     cndSecond _
                   )
  End If
  Set GetElement = elmParent.FindFirst(TreeScope_Subtree, cndFirst)
End Function

こちらはInternet Explorerのタブ列を取得した上で、SelectionItem.IsSelectedプロパティ(UIA_SelectionItemIsSelectedPropertyId)からアクティブなタブを判断しています。
タブを閉じる処理は、タブ内にある閉じるボタンをクリックしているだけです。

以上のように2パターンの処理を書いてみました。
安定性の上ではUI Automationを使ったコードの方が高いのですが、若干コードが冗長になりますので、そこまで精度を問わないのであれば、「CloseCurrentIeTab1」の方で良いだろうと思います。

関連Webページ

信号機ガチャ第2弾コンプしました。前のページ

[Google Apps Script]メールからMessage-IDヘッダーを取得する次のページ

関連記事

  1. Office関連

    VBAプロジェクトを「展開する」VBAマクロ

    MSDNフォーラムに面白い質問がありました。VBE・プロジェクト …

  2. Excel

    Google TTSで文字列を読み上げるExcelアドイン

    前回の記事で書いたGoogle TTSで文字列を読み上げるマクロ(言語…

  3. Office関連

    PowerPointの自動実行マクロ

    ExcelのAuto_OpenやWordのAutoOpenのように、P…

  4. Office関連

    フッターにページ番号と総ページ数を挿入するWordマクロ

    以前書いた、フッターに「ページ番号 / 総ページ数」を挿入するWord…

  5. Excel

    [VBA]自動的にフォントサイズを調整する疑似テキストボックス

    前回と同様、環境依存つながりでmougの給湯室に書いたコードを載せてお…

  6. Office関連

    【2017年1月版】Microsoft Edgeを操作するVBAマクロ(DOM編)

    2021/10/1 追記:本記事は公開されてから大分時間が経ってお…

コメント

  • コメント (0)

  • トラックバックは利用できません。

  1. この記事へのコメントはありません。

Time limit is exhausted. Please reload CAPTCHA.

※本ページはプロモーションが含まれています。

Translate

最近の記事

アーカイブ

PAGE TOP