VBA 恢复现有的 IE
VBA to restore existing IE
下面的代码
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
我只设法激活了 windows,但当它在任务栏中时无法调出它。无法找到任何要恢复的代码。
尝试显示 window 但不工作
Private Declare Function ShowWindow Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal nCmdShow As SHOW_WINDOW) As Boolean
下面是一些将执行以下操作的代码:
1) 通过部分位置名称找到 Internet Explorer window。 (使用 InStr())
2) 恢复使用 ShowWindow 找到的 window,然后使用 SetForegroundWindow windows API
激活
这是代码,我在我的计算机上使用 Internet Explorer 对此进行了测试。效果很好。
确保将 WindowName
变量更新为您正在搜索的 window 的名称。
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
下面的代码
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
我只设法激活了 windows,但当它在任务栏中时无法调出它。无法找到任何要恢复的代码。
尝试显示 window 但不工作
Private Declare Function ShowWindow Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal nCmdShow As SHOW_WINDOW) As Boolean
下面是一些将执行以下操作的代码:
1) 通过部分位置名称找到 Internet Explorer window。 (使用 InStr())
2) 恢复使用 ShowWindow 找到的 window,然后使用 SetForegroundWindow windows API
激活这是代码,我在我的计算机上使用 Internet Explorer 对此进行了测试。效果很好。
确保将 WindowName
变量更新为您正在搜索的 window 的名称。
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