首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >恢复现有IE的VBA

恢复现有IE的VBA
EN

Stack Overflow用户
提问于 2016-08-25 12:11:09
回答 1查看 885关注 0票数 0

下面的代码

代码语言:javascript
复制
    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

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

尝试显示窗口,但不起作用

代码语言:javascript
复制
 Private Declare Function ShowWindow Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal nCmdShow As SHOW_WINDOW) As Boolean
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-29 21:39:07

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

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

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

这是代码,我在我的电脑上使用Internet Explorer对其进行了测试。它工作得很好。

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

代码语言:javascript
复制
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
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39136589

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档