7

私が作成しているマクロは、Excel スプレッドシートから名前を取得し、Internet Explorer を開き、オンライン ディレクトリを検索します。ディレクトリを検索した後、管理者の名前を含む Java フォームを表示します。マネージャー名に手動でタブ移動し、右クリックしてショートカットをコピーし、それをスプレッドシートに戻すことができます。ただし、一貫したタブ移動とショートカットのコピーに問題があります。

  1. IE ウィンドウにフォーカスを戻す簡単な方法はありますか?
  2. 手動でクリックせずにショートカットをコピーするにはどうすればよいですか?

コード:

Sub Macro1()
'
Dim ie As Object
Set ie = CreateObject("internetexplorer.application")

ie.Visible = True
ie.navigate "****url****"

While ie.busy
    DoEvents
Wend

ie.document.getElementById("SSOID").Value = "Z19516732"
ie.document.getElementById("Advanced").Checked = False
ie.document.all("Search").Click

'this loop is to slow the macro as the java form is filled from the search
For i = 1 To 400000000  
    i = i + 1
Next i

'ie.Object.Activate
ie.document.getElementById("Advanced").Checked = False
ie.document.getElementById("SSOID").Focus
Application.SendKeys "{TAB 6}" ', True

'bring up the control menu/right click
Application.SendKeys "+{F10}"

'copy shortcut is 8 items down on the list
Application.SendKeys "{DOWN}"
Application.SendKeys "{DOWN}"
Application.SendKeys "{DOWN}"
Application.SendKeys "{DOWN}"
Application.SendKeys "{DOWN}"
Application.SendKeys "{DOWN}"
Application.SendKeys "{DOWN}"
Application.SendKeys "{DOWN}"

'enter was not working so the shortcut for the menu is 't'
'SendKeys "{ENTER}"
Application.SendKeys "{t}"

Windows("Book21").Activate
Range("A1").Select
ActiveSheet.Paste

End Sub
4

1 に答える 1

9

モジュールの先頭に、次のコード行を配置します。

Public Declare Function SetForegroundWindow Lib "user32" (ByVal HWND As Long) As Long

これはと呼ばれ、Windowsに組み込まれている機能Declare Statementにアクセスできるようになります。SetForegroundWindowこの関数user32は、WindowsシステムのDLLにあります。実際には、この方法でVBAにアクセスできる複数のDLLの中には、他にも多くの機能があります(その他の例についてはリンクを参照してください)。

コードで、IEオブジェクトを操作しながら、次のようにHWND(そのウィンドウへのハンドル)を記録します。

Dim HWNDSrc As Long
HWNDSrc = ie.HWND

次に、Javaを操作した後、これを使用して続行します。

SetForegroundWindow HWNDSrc

これは、WindowsシステムにHWND(名前が示すように)フォアグラウンドウィンドウとして識別されるウィンドウを設定するように指示します。

ただし、IEとの対話方法によっては、これは必要ない場合があります。つまり、ウィンドウを表示/タッチする必要がない場合でも、コードに既にあるように、オブジェクトを使用して対話できます。

GetElementById()andのようなコードを使用して探しているショートカットを取得する方法はいくつかありますがGetElementsByTagName()詳細についてはこちらを参照)、ソースがどのように作成されたかによって異なります。たとえば、<a href="...>HTMLソースを知っている場合、リンクは比較的簡単にプルできるはずです。


コードをもう一度確認した後、ループを使用してマクロを「スローダウン」していることに気付きました。自分の似たような方法でいつも使っている機能があります。うまくいけば、これはあなたが必要なことを成し遂げるのに役立つでしょう。あなたのケースには当てはまらない追加の詳細があったので、以下のコードを自分のオリジナルから変更しました。エラーがあれば、必要に応じて調整できます。

Public Sub WaitForIE(myIEwindow As InternetExplorer, HWND As Long, WaitTime As Integer)

    ' Add pauses/waits so that window action can actually
    ' begin AND finish before trying to read from myIEWindow.

    ' myIEWindow is the IE object currently in use
    ' HWND is the HWND for myIEWindow
    ' The above two variables are both used for redundancy/failsafe purposes.
    ' WaitTime is the amount of time (in seconds) to wait at each step below. 
    ' This is variablized because some pages are known to take longer than 
    ' others to load, and some pages with frames may be partially loaded,
    ' which can incorrectly return an READYSTATE_COMPLETE status, etc.

    Dim OpenIETitle As SHDocVw.InternetExplorer

    Application.Wait DateAdd("s", WaitTime, Now())

    Do Until myIEwindow.ReadyState = READYSTATE_COMPLETE
        ' Wait until IE is done loading page and/or user actions are done.
    Loop

    Application.Wait DateAdd("s", WaitTime, Now())

    While myIEwindow.Busy
        DoEvents  ' Wait until IE is done loading page and/or user actions are done.
    Wend

    On Error Resume Next
    ' Make sure our window still exists and was not closed for some reason...
    For Each OpenIETitle In objShellWindows
        If OpenIETitle.HWND = HWND Then
            If Err.Number = 0 Then
                Set myIEwindow = OpenIETitle
                Exit For
            Else
                Err.Clear
            End If
        End If
    Next OpenIETitle
    On Error GoTo 0

End Sub
于 2012-07-25T15:43:02.010 に答える