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関連

    文書が互換モードかどうかを判定するWordマクロ

    古いバージョンのWordで作成された文書を開くと、タイトル バーに「互…

  2. Office アドイン

    [Office用アプリ]カレンダーから日付入力

    カレンダーから日付を選ぶだけで選択中のセルに日付を入力できるコンテンツ…

  3. Office関連

    「ExcelVBAを実務で使い倒す技術」レビュー

    @ateitexeさんの下記ツイートで、高橋宣成氏が執筆された「Exc…

  4. Office関連

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

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

  5. Office関連

    スライド内容を自動的に機械翻訳するPowerPointマクロ

    前回の記事で紹介した各スライドに配置されたオートシェイプからテキストを…

コメント

  • コメント (0)

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

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

Time limit is exhausted. Please reload CAPTCHA.

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

Translate

最近の記事

アーカイブ

PAGE TOP