簡體   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