简体   繁体   English

VBA与互联网浏览器的互动

[英]VBA interaction with internet explorer

The macro I am building takes names from an Excel spreadsheet, opens Internet Explorer, and searches the online directory. 我正在构建的宏从Excel电子表格中获取名称,打开Internet Explorer,然后搜索在线目录。 After searching the directory, it pulls up a Java form with the manager's name in it. 在搜索目录之后,它会提取一个包含管理器名称的Java表单。 I am able to manually tab to the manager name, right click, copy shortcut, and then post it back on the spread sheet. 我可以手动选项卡到管理器名称,右键单击,复制快捷方式,然后将其发布回电子表格。 However, I am having problems with consistent tabbing and copying the shortcut. 但是,我遇到了一致标签和复制快捷方式的问题。

  1. Is there a simple way of bringing focus back onto the IE window? 有没有一种简单的方法将焦点带回IE窗口?
  2. How do you copy a shortcut without manually clicking it? 如何在不手动单击的情况下复制快捷方式?

Code: 码:

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

At the beginning of your module, put this line of code: 在你的模块的开头,把这行代码:

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

This is called a Declare Statement and will allow you to access the SetForegroundWindow function that is built into Windows. 这称为Declare Statement ,允许您访问Windows内置的SetForegroundWindow函数。 This function lives in the user32 DLL of the Windows system. 此函数位于Windows系统的user32 DLL中。 There are actually a number of other functions among multiple DLLs that are accessible to VBA in this way (see link for more examples). 实际上,VBA可以通过这种方式访问​​多个DLL中的许多其他函数(有关更多示例,请参阅链接)。

In your code, while interacting with your IE object, record the HWND ( handle to that window) like so: 在你的代码,而与你的IE对象交互,记录HWND句柄到窗口),如下所示:

Dim HWNDSrc As Long
HWNDSrc = ie.HWND

Then, after you've interacted with Java, use this to continue: 然后,在与Java交互之后,使用它继续:

SetForegroundWindow HWNDSrc

This tells the Windows system to set the window identified by the HWND as the foreground window (as the name implies). 这告诉Windows系统将HWND标识的窗口设置为前景窗口(顾名思义)。

However, this may not be necessary, depending on how you are interacting with IE. 但是,这可能没有必要,具体取决于您与IE的交互方式。 In other words, if you don't need to see/touch the window, you can still interact using the object as you have in your code already. 换句话说,如果您不需要查看/触摸窗口,您仍然可以使用该代码中的对象进行交互。

There are ways to get the shortcut you are looking for using code like GetElementById() and GetElementsByTagName() ( see here for more info ), but it will depend on how the source was created. 有一些方法可以使用GetElementById()GetElementsByTagName()等代码获取您正在寻找的快捷方式( 有关详细信息请参阅此处 ),但这取决于如何创建源。 eg an <a href="...> link should be relatively easy to pull, if you know the HTML source. 例如,如果你知道HTML源代码,那么<a href="...>链接应该相对容易拉动。


After reviewing your code a second time, I noticed you use a loop to 'slow down' the macro. 在第二次检查您的代码后,我注意到您使用循环来“减慢”宏。 I have a function I use all the time for similar methods of my own. 我有一个功能,我一直使用自己的类似方法。 Hopefully this will help in you getting done what you need. 希望这有助于您完成所需的工作。 I've modified my code below from my own original, since I had additional specifics that don't apply to your case. 我已经从我原来的代码中修改了我的代码,因为我有其他细节不适用于您的情况。 If there are any errors with it, I can adjust as needed. 如果有任何错误,我可以根据需要进行调整。

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