下面的代码
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发布于 2016-08-29 21:39:07
下面是将执行以下操作的一些代码:
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 Subhttps://stackoverflow.com/questions/39136589
复制相似问题