VBA恢复现有IE

时间:2016-08-25 04:11:09

标签: vba explorer

以下代码

    Set objShell = CreateObject("Shell.Application")
IE_count = objShell.Windows.Count
For x = 0 To (IE_count - 1)
    On Error Resume Next    ' sometimes more web pages are counted than are open
    my_url = objShell.Windows(x).document.Location
    my_title = objShell.Windows(x).document.Title
    If my_title Like "*" & "New" Then 
        Set IE = objShell.Windows(x)
AppActivate my_title

我只设法激活窗口但是当它在任务栏中时无法调用它。无法找到任何要还原的代码。

尝试了显示窗口但没有工作

 Private Declare Function ShowWindow Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal nCmdShow As SHOW_WINDOW) As Boolean

1 个答案:

答案 0 :(得分:1)

下面是一些将执行以下操作的代码:

1)通过部分位置名称找到Internet Explorer窗口。 (使用InStr())

2)使用ShowWindow恢复找到的窗口,然后使用SetForegroundWindow windows API激活

这是代码,我在计算机上使用Internet Explorer对此进行了测试。它运作良好。

确保将WindowName变量更新为您要搜索的窗口的名称。

Private Const SW_SHOWNORMAL = 1
Private Const SW_MAXIMIZE = 3
Private Const SW_SHOW = 5
Private Const SW_MINIMIZE = 6
Private Const SW_RESTORE = 9

#If VBA7 Then
    Public Declare PtrSafe Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare PtrSafe Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
    Private Declare PtrSafe Function IsIconic Lib "user32.dll" (ByVal hwnd As Long) As Long
#Else
    Public Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
    Private Declare Function IsIconic Lib "user32.dll" (ByVal hwnd As Long) As Long
#End If

Public Sub Activate_A_Window()
    Dim IE As Object
    Dim Windows As Object: Set Windows = CreateObject("Shell.Application").Windows
    Dim Window As Object
    Dim my_title As String
    Dim WindowName As String: WindowName = "WindowNameGoesHere"

    For Each Window In Windows
        my_title = Window.LocationName
        If InStr(1, my_title, WindowName) Then
            Set IE = Window
            Exit For
        End If
    Next Window

    If Not IE Is Nothing Then 'Make sure IE was found as a window
        If CBool(IsIconic(IE.hwnd)) Then ' If it's minimized, show it
            ShowWindow IE.hwnd, SW_RESTORE
        End If

        SetForegroundWindow IE.hwnd 'Set the window as the foreground
    Else
        MsgBox (WindowName & " could not be located")
    End If

End Sub