在 Excel VBA 中列出打开的窗口的最简单方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15424320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 20:03:29  来源:igfitidea点击:

What's the easiest way to list out the open windows in Excel VBA?

excelvba

提问by stanigator

I know I could've been searching on Google, but I've been rusty at it to the point where I'm not sure what terms I should search for. It would help to hear your suggestions here.

我知道我可以在谷歌上搜索,但我已经生疏了,以至于我不确定我应该搜索什么词。在这里听取您的建议会有所帮助。

Example:

例子:

Sub Macro1()
'
' Macro1 Macro
'

'
    Windows("FL_bounces.csv").Activate
    Windows("auto_dealers_FL.csv").Activate
    Windows("FL_bounces.csv").Activate
    Windows("auto_dealers_FL.csv").Activate
End Sub

Except where I don't know the names of the windows open.

除了我不知道打开的窗户的名字的地方。

Edit #2:

编辑#2:

Sub Macro1()
'
' Macro1 Macro
'

'
    Dim wn, contacts, report As Excel.Window
    Dim windows(1 To 100) As Excel.Window
    Dim i As Integer

    i = 1
    For Each wn In Application.windows
        windows(i) = wn
        i = i + 1
    Next wn

    If IsEmailValid(windows(1).Cells(1, 1)) = True Then
        report = windows(1)
        contacts = windows(2)
    Else
        contacts = windows(1)
        report = windows(2)
    End If


End Sub

What do you see wrong in this modification?

你觉得这个修改有什么问题?

回答by Doug Glancy

This will list all the currently open windows in the Immediate Pane:

这将在即时窗格中列出所有当前打开的窗口:

Sub ListWindows()
Dim wn As Excel.Window
For Each wn In Application.Windows
    Debug.Print wn.Caption
Next wn
End Sub

Or, if you want to activate them, as in your sample code

或者,如果您想激活它们,如示例代码中所示

Sub ActivateWindows()
Dim wn As Excel.Window
For Each wn In Application.Windows
    wn.Activate
    MsgBox wn.Caption & " Window Activated"
Next wn
End Sub

回答by Doug Glancy

Try AutoIT. You need reference to AutoIT dll. Here is link from where you can download autoit dll http://www.autoitscript.com/site/autoit/

试试 AutoIT。您需要参考 AutoIT dll。这是您可以下载 autoit dll http://www.autoitscript.com/site/autoit/ 的链接

The variant datatype in AutoIt natively supports window handles (HWNDs). A window handle is a special value that windows assigns to a window each time it is created. When you have a handle you may use it in place of the title parameter in any of the function calls that use the title/text convention. The advantage of using window handles is that if you have multiple copies of an application open - which have the same title/text - you can uniquely identify them when using handles. When you use a window handle for the title parameter then the text parameter is completely ignored.

AutoIt 中的变体数据类型本身支持窗口句柄 (HWND)。窗口句柄是窗口每次创建时分配给窗口的特殊值。当您有句柄时,您可以在任何使用标题/文本约定的函数调用中使用它代替标题参数。使用窗口句柄的优点是,如果您打开了一个应用程序的多个副本 - 它们具有相同的标题/文本 - 您可以在使用句柄时唯一地标识它们。当您为标题参数使用窗口句柄时,文本参数将被完全忽略。

Various functions such as WinGetHandle, WinList and GUICreate return these handles. It is important to note that a window handle is not classed as a number or string - it is its own special type.

WinGetHandle、WinList 和 GUICreate 等各种函数返回这些句柄。需要注意的是,窗口句柄不属于数字或字符串——它是它自己的特殊类型。

Public Sub TestingAutoIT()
    Dim autoItObj As AutoItX3
    Set autoItObj = New AutoItX3

    With autoItObj    
        .WinActivate ("A Window Name")        
    End With

    Set autoItObj = Nothing
End Sub

回答by elliot svensson

Today I found a solution posted by VBA Express. It will list all the open windows on the activesheet.

今天我找到了VBA Express 发布的解决方案。它将列出活动表上所有打开的窗口。

Note that "a window" is not necessarily what the user normally thinks of... some windows are collections of many windows, the way Windows sees it.

请注意,“一个窗口”不一定是用户通常想到的……有些窗口是许多窗口的集合,Windows 是这样看待它的。