はVBA

2017-05-24 10 views
0

で、Internet Explorerの[背景]タブをアクティブにすると、私は次のコードスニペットを持っていると仮定します。はVBA

Option Explicit 

Sub Main() 
    Dim IE As Object 
    Set IE = GetIE("http://www.google.com") 

    ' Need tabbed page for google.com to come to the foreground 

End Sub 

GetIE関数はとは対照的に、GetObjectを使用して参照私と一緒に

Function GetIE(sLocation As String) As Object 

    Dim objShell As Object, objShellWindows As Object, o As Object 
    Dim sURL As String 
    Dim retVal As Object 

    Set retVal = Nothing 
    Set objShell = CreateObject("shell.application") 
    Set objShellWindows = objShell.Windows 

    For Each o In objShellWindows 
     sURL = "" 
     On Error Resume Next 
     'check the URL and if it's the one you want then 
     'assign it to the return value and exit the loop 
     sURL = o.document.location 
     On Error GoTo 0 
     If sURL Like sLocation & "*" Then 
      Set retVal = o 
      Exit For 
     End If 
    Next o 

    Set GetIE = retVal 

End Function 

については、以下でありますCreateObjectオブジェクトが実際にInternet Explorerアプリケーション内のタブである状況が発生することがあります。

問題は私のオートメーションの目的のためです。私は通常、いつでもかなりの数のタブを開いています。 IEオブジェクトを作成して、タブ付きのページwww.google.comをフォアグラウンド(表示タブにする)にする方法はありますか?私はIE.ActivateIE.Visible = TrueIE.Document.Focusを試しましたが、私が必要とする結果を得ることができません。

Internet Explorerアプリケーションで表示されているタブではなく、私が必要とする(IE.Navigateと要素をフックしてgetElementByIDを使用する)他の方法でタブを操作できることを追加する必要があります。

答えて

0

以下は、スタンドアロンのウィンドウでも、タブでも、Internet Explorerウィンドウの既存のインスタンスへの参照を取得するために使用するものです。

私が気づいた違いは、LocationURLまたはLocationNameプロパティを使用してウィンドウを識別したい場合です。 Internet ExplorerのドキュメントでLocationプロパティを見つけられないようでした(しかし、私はそれを見逃している可能性があります)ので、直面している問題の一部である可能性があります。

Function getIE(SearchBy As String, SearchCriteria As String) As Object 
On Error GoTo Errhand: 

    Dim Window As Object 

    For Each Window In CreateObject("Shell.Application").Windows 
     If SearchBy = "URL" And Window.LocationUrl Like "*" & SearchCriteria & "*" Then 
      Set getIE = Window 
      Exit Function 
     ElseIf SearchBy = "Name" And Window.LocationName Like "*" & SearchCriteria & "*" Then 
      Set getIE = Window 
      Exit Function 
     End If 
    Next 

CleanExit: 
    Set getIE = Nothing 
    Exit Function 

Errhand: 
    'Add error handling/messaging here 
    Resume CleanExit 
End Function 

上記のコードは、共有したコードと同じように動作します。 1つの重要な特徴は、一致が見つからない場合、オブジェクトはNothingとして返されることです。あなたはそのウィンドウへの参照を持っていたら、それは使用にの一部です方法を言うためにアクティブである必要はありません

Sub SOIE_Example() 
    Dim IE As Object 
    Set IE = getIE("URL", "google") 

    If IE Is Nothing Then 
     MsgBox ("Couldn't find the IE window with the title google") 
     Exit Sub 
    Else 
     'Do what you need with the object/website here 
    End If 
End Sub 

:あなたのコードに統合するときに、それはあなたがこれを行うことができ、あなたはこれをチェック重要ですInternet Explorerオブジェクトです。そのウィンドウがアクティブでなくても、HTMLElementの値、innerText、リンク/ボタンなどを更新することができます。

+0

オブジェクトの設定に問題はありません。IEブラウザでタブをアクティブにしてアクティブなウィンドウにする方法を知りたいだけです。したがって、私が自動化しているタブがバックグラウンドであった場合、どのようにフォアグラウンドに持ってきて、タブの内容をブラウザに表示するのですか? –