与Internet Explorer VBA互动 [英] VBA interaction with internet explorer

查看:225
本文介绍了与Internet Explorer VBA互动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立宏从Excel US preadsheet需要的名字,打开IE浏览器,并搜索在线目录。搜索目录后,它拉起来的经理在名字的Java的形式。我能手动选项卡,经理姓名,右击,复制快捷方式,然后上传回的S $ P $垫纸。不过,我有一致的跳格的问题和复制的快捷方式。


  1. 有没有把焦点放回IE窗口的一个简单的方法?

  2. 如何复制快捷方式,而无需手动单击它?

code:

 子Macro1的()

昏暗的IE作为对象
设置IE =的CreateObject(internetexplorer.application)ie.Visible = TRUE
ie.navigate**** ****网址虽然ie.busy
    的DoEvents
WENDie.document.getElementById(SSOID)。值=Z19516732
ie.document.getElementById(高级)。选中=假
ie.document.all(搜索)。点击'该环是减缓宏作为Java形式从搜索填充
对于i = 1到4亿
    I = I + 1
接下来,我ie.Object.Activate
ie.document.getElementById(高级)。选中=假
ie.document.getElementById(SSOID)。重点
Application.SendKeys{TAB 6}',真调出控制菜单/右键单击
Application.SendKeys+ {} F10复制快捷方式是8个项目在清单上
Application.SendKeys{} DOWN
Application.SendKeys{} DOWN
Application.SendKeys{} DOWN
Application.SendKeys{} DOWN
Application.SendKeys{} DOWN
Application.SendKeys{} DOWN
Application.SendKeys{} DOWN
Application.SendKeys{} DOWN确认没有工作,所以对于快捷菜单中的T
'的SendKeys{ENTER}
Application.SendKeys横置窗口(Book21)。激活
范围(A1)。选择
ActiveSheet.Paste结束小组


解决方案

在你的模块开始,就把这条线code的:

 公开声明函数库SetForegroundWindowUSER32(BYVAL HWND长)只要

这就是所谓的 声明声明 并允许你来访问Windows中内置的 SetForegroundWindow 功能。这个函数住在Windows系统的 USER32 DLL。其实有许多可以访问的VBA以这种方式(见链接更多的例子)。

多个DLL之间等功能

在您的code,同时与你的IE对象进行交互,记录 HWND 手柄到窗口),像这样:

 昏暗HWNDSrc只要
HWNDSrc = ie.HWND

然后,在你与Java交互后,使用此继续:

  SetForegroundWindow HWNDSrc

这告诉Windows系统的设置由 HWND 确定为前台窗口的窗口(顾名思义)。

不过,这可能不是必要的,这取决于你如何使用IE浏览器进行交互。换句话说,如果你不需要看/触摸窗口,仍可以交互使用对象是你在code已经。

有办法得到你要寻找一个使用code像的getElementById()的getElementsByTagName()看到这里获取更多信息),但是这将取决于源是如何创建的。例如一个< A HREF =...方式> 链接应该是比较容易拉,如果你知道HTML源代码


审查您的code第二次之后,我注意到你使用一个循环为慢下来的宏。我有我利用一切为我自己的类似的方法,时间的函数。希望这将有助于你得到完成你所需要的。我修改我的下面code从我自己的原创,因为我已经不适用于您的情况下,更多的细节。如果有任何与它的错误,如需要,我可以调整。

 公用Sub WaitForIE(myIEwindow作为InternetExplorer的,HWND长,WAITTIME作为整数)    添加暂停/等待,因此,该窗口的行动实际上可以
    开始并试图从myIEWindow读取前完成。    myIEWindow是IE对象当前正在使用中
    HWND是myIEWindow的HWND
    '以上两个变量都用于冗余/故障安全的目的。
    'WAITTIME是时间的量(以秒计),等待在下面每一个步骤。
    这是因为variablized一些网页被称为拿长于
    别人加载,以及一些网页使用框架可能是部分加载,
    这可能会错误地返回一个READYSTATE_COMPLETE状态等。    昏暗OpenIETitle作为SHDocVw.InternetExplorer    Application.Wait使用DateAdd(S,WAITTIME,现在())    做,直到myIEwindow.ReadyState = READYSTATE_COMPLETE
        等到IE加载完成页面和/或用户操作完成。
    循环    Application.Wait使用DateAdd(S,WAITTIME,现在())    虽然myIEwindow.Busy
        调用DoEvents等到IE加载完成页面和/或用户操作完成。
    WEND    在错误恢复下一页
    确保我们的窗口依然存在,并没有关闭出于某种原因...
    对于每个OpenIETitle在objShellWindows
        如果OpenIETitle.HWND = HWND然后
            如果Err.Number的= 0则
                设置myIEwindow = OpenIETitle
                对于出口
            其他
                Err.Clear
            万一
        万一
    接下来OpenIETitle
    对错误转到0结束小组

The macro I am building takes names from an Excel spreadsheet, opens Internet Explorer, and searches the online directory. After searching the directory, it pulls up a Java form with the manager's name in it. 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?
  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. This function lives in the user32 DLL of the Windows system. There are actually a number of other functions among multiple DLLs that are accessible to VBA in this way (see link for more examples).

In your code, while interacting with your IE object, record the HWND (handle to that window) like so:

Dim HWNDSrc As Long
HWNDSrc = ie.HWND

Then, after you've interacted with Java, use this to continue:

SetForegroundWindow HWNDSrc

This tells the Windows system to set the window identified by the HWND as the foreground window (as the name implies).

However, this may not be necessary, depending on how you are interacting with 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. e.g. an <a href="...> link should be relatively easy to pull, if you know the HTML source.


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

这篇关于与Internet Explorer VBA互动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆