简体   繁体   English

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

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

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?你觉得这个修改有什么问题?

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

Try AutoIT.试试 AutoIT。 You need reference to AutoIT dll.您需要参考 AutoIT dll。 Here is link from where you can download autoit dll http://www.autoitscript.com/site/autoit/这是您可以下载 autoit dll http://www.autoitscript.com/site/autoit/ 的链接

The variant datatype in AutoIt natively supports window handles (HWNDs). AutoIt 中的变体数据类型本身支持窗口句柄 (HWND)。 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.当您为标题参数使用窗口句柄时,文本参数将被完全忽略。

Various functions such as WinGetHandle, WinList and GUICreate return these handles. WinGetHandle、WinList 和 GUICreate 等各种函数返回这些句柄。 It is important to note that a window handle is not classed as a number or string - it is its own special type.需要注意的是,窗口句柄不属于数字或字符串——它是它自己的特殊类型。

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

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

    Set autoItObj = Nothing
End Sub

Today I found a solution posted by VBA Express .今天我找到了VBA Express 发布的解决方案。 It will list all the open windows on the activesheet.它将列出活动表上所有打开的窗口。

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 是这样看待它的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM