繁体   English   中英

VBA 激活 Internet Explorer Window

[英]VBA to Activate Internet Explorer Window

我正在制作一个可以打开 Internet Explorer、导航和登录网站的宏。 一切正常,但我需要把 IE Window 放在前面,然后激活它,这样我就可以使用SendKeys了。 我在一个名为AppActivate的命令上找到了具有不同方法的网站和视频,我已经尝试了很多,但即使我复制了整个代码(对作者有用),它对我也不起作用,我总是得到错误:无效的过程调用或参数 - 错误 5

我找到并尝试过的所有内容的列表:

Dim objIE As InternetExplorerMedium
Set objIE = New InternetExplorerMedium
objIE.navigate "http://google.com"
'makes it visible, but not active
objIE.Visible = True

'A list of ways I've tried:
objIE.AppActivate "Google - Internet Explorer"
AppActivate "Google - internet Explorer"
'the above supposedly looks for the title of the page
AppActivate objIE
AppActivate (objIE)
AppActivate "objIE"

观察:我在 Excel 2013 中运行它,我正在使用 Windows 7 和 IE 11。

我总是像这样将IE设为无类型对象

Sub test()
Dim IE As Object

Set IE = Nothing
Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate "www.google.com"
IE.Visible = True

End Sub

完成此操作后,IE窗口将具有焦点。

有人在这里写类似的东西:

通过 VBA 获取现有 IE

稍加修改: (SetForegroundWindow Lib "user32" ) 这样搜索Existing IE后,它就会出现在我们的屏幕顶部

*PtrSafe / LongPtr 用于 64 位系统 *如果您使用 32 位系统,您可以删除它


调用库

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

主副

Sub Test1()

    Dim IE1 As Object
    Set IE1 = ThisIE
    
    IE1.navigate "http://stackoverflow.com"
    
    Do While IE1.readyState <> READYSTATE_COMPLETE
    Loop
    
    SetForegroundWindow (IE1.HWND)
End Sub

Function 被调用

Function ThisIE() As Object

    For Each ThisIE In CreateObject("Shell.Application").Windows()
        If (Not ThisIE Is Nothing) And ThisIE.Name = "Internet Explorer" Then Exit For
    Next ThisIE
    
    If ThisIE Is Nothing Then Set ThisIE = CreateObject("InternetExplorer.Application")
        ThisIE.Visible = True
        
End Function

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM