我建立宏从Excel电子表格需要的名字,打开IE浏览器,并搜索在线目录。 搜索目录后,它拉了经理在它的名字一个Java形式。 我能手动选项卡管理器名称,右击,复制快捷方式,然后将它张贴回试算表。 但是,我有一致的黏合问题和复制的快捷方式。
- 有没有把焦点放回IE窗口的一个简单的方法?
- 你如何复制无需手动点击它的快捷方式?
码:
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
,将允许您访问SetForegroundWindow
被内置到Windows功能。 这个函数住在user32
Windows系统的DLL。 实际上有多个DLL可以访问的VBA以这种方式(见链接,更多的例子)中,具有一些其他的功能。
在你的代码,而与你的IE对象交互,记录HWND
( 句柄到窗口),如下所示:
Dim HWNDSrc As Long
HWNDSrc = ie.HWND
然后,在你与Java交互后,使用它来继续:
SetForegroundWindow HWNDSrc
这就告诉Windows系统设置被识别的画面HWND
为前台窗口(顾名思义)。
然而,这可能不是必要的,这取决于你如何使用IE浏览器进行交互。 换句话说,如果你不需要看/触摸窗口,你仍然可以使用互动的对象,你在你的代码已经。
有办法得到快捷你正在寻找使用如下代码GetElementById()
和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