copyshortcut功能滞后/落后

时间:2012-07-27 17:49:45

标签: vba excel-vba clipboard copy-paste excel

我正在编写一个宏来进入Internet Explorer,搜索和员工编号,从该搜索中找到管理器,最后将其复制+粘贴到工作簿中的单元格....但是,它似乎总是落后于“复制”的人。我的意思是,如果我在运行宏之前复制一些东西,然后运行它,复制的内容就会出现在单元格中。然而,我再次点击运行,它复制了经理名称。它似乎永远是一个“副本”,或者选择剪贴板上的第二个最新项目。此外,如果我在复制和粘贴coade之前清除clipbaord,我会收到错误。这是为什么?

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

Dim HWNDSrc As Long
HWNDSrc = ie.HWND   'to setup for focusing internet explorer

ie.Visible = True

ie.navigate "http://url.com/" 'address to find

While ie.Busy   'loop until ie is done loading
     DoEvents
Wend

Call WaitForIE(ie, HWNDSrc, 7) 'to check and make sure ie is done loading

ie.document.getElementById("SSOID").Value = "m1z016p32" 'input into search box

ie.document.getElementById("SSOID").Select
SetForegroundWindow HWNDSrc 'focuses the active application
Application.SendKeys "~"    'enter key
ie.document.getElementById("Advanced").Checked = False  'make sure the advanced box is unchecked


For i = 1 To 200000000      'loop to load the search
i = i + 1
Next i


ie.document.getElementById("Advanced").Checked = False

ie.document.getElementById("SSOID").Select  'focuses the cursor so the tabs will align

SetForegroundWindow HWNDSrc
Application.SendKeys "{TAB 6}"  'tab to location

Application.SendKeys "+{F10}"   'right click on the manger name
Application.SendKeys "{DOWN}"   'goes down to 'copyshortcut'
Application.SendKeys "{DOWN}"

Application.SendKeys "{DOWN}"
Application.SendKeys "{DOWN}"
Application.SendKeys "{DOWN}"
Application.SendKeys "{DOWN}"
Application.SendKeys "{DOWN}"
Application.SendKeys "{DOWN}"
Application.SendKeys "~"        '[presses enter

Windows("Book21").Activate      'workbook o activate
Range("A1").Select          'selects the cell
ActiveSheet.Paste           'past the data



End Sub`

1 个答案:

答案 0 :(得分:2)

好的,这就是逻辑。

导航到网址后,找到您需要输入SSO和密码的HTML对象,然后使用.Value简单地传递它们。我知道你已经这样做了。

接下来是查找Submit按钮并使用.Click代替Application.SendKeys "~"来模拟点击事件

到达目标网址后,找到对象的元素ID,然后检索它的值并将其写入excel文件,而不是选项卡。

你已经掌握了大部分内容。您需要做的就是不使用sendkeys,而是直接与浏览器元素交互。

您可能希望看到this。一个有趣的读物,展示了如何将数据检索到Excel。

html parsing of cricinfo scorecards