簡體   English   中英

VBA與互聯網瀏覽器的互動

[英]VBA interaction with internet explorer

我正在構建的宏從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

在你的模塊的開頭,把這行代碼:

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

這稱為Declare Statement ,允許您訪問Windows內置的SetForegroundWindow函數。 此函數位於Windows系統的user32 DLL中。 實際上,VBA可以通過這種方式訪問​​多個DLL中的許多其他函數(有關更多示例,請參閱鏈接)。

在你的代碼,而與你的IE對象交互,記錄HWND句柄到窗口),如下所示:

Dim HWNDSrc As Long
HWNDSrc = ie.HWND

然后,在與Java交互之后,使用它繼續:

SetForegroundWindow HWNDSrc

這告訴Windows系統將HWND標識的窗口設置為前景窗口(顧名思義)。

但是,這可能沒有必要,具體取決於您與IE的交互方式。 換句話說,如果您不需要查看/觸摸窗口,您仍然可以使用該代碼中的對象進行交互。

有一些方法可以使用GetElementById()GetElementsByTagName()等代碼獲取您正在尋找的快捷方式( 有關詳細信息請參閱此處 ),但這取決於如何創建源。 例如,如果你知道HTML源代碼,那么<a href="...>鏈接應該相對容易拉動。


在第二次檢查您的代碼后,我注意到您使用循環來“減慢”宏。 我有一個功能,我一直使用自己的類似方法。 希望這有助於您完成所需的工作。 我已經從我原來的代碼中修改了我的代碼,因為我有其他細節不適用於您的情況。 如果有任何錯誤,我可以根據需要進行調整。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM