簡體   English   中英

在 Excel VBA 中列出打開的窗口的最簡單方法是什么?

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

我知道我可以在谷歌上搜索,但我已經生疏了,以至於我不確定我應該搜索什么詞。 在這里聽取您的建議會有所幫助。

例子:

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

除了我不知道打開的窗戶的名字的地方。

編輯#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

你覺得這個修改有什么問題?

這將在即時窗格中列出所有當前打開的窗口:

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

或者,如果您想激活它們,如示例代碼中所示

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

試試 AutoIT。 您需要參考 AutoIT dll。 這是您可以下載 autoit dll http://www.autoitscript.com/site/autoit/ 的鏈接

AutoIt 中的變體數據類型本身支持窗口句柄 (HWND)。 窗口句柄是窗口每次創建時分配給窗口的特殊值。 當您有句柄時,您可以在任何使用標題/文本約定的函數調用中使用它代替標題參數。 使用窗口句柄的優點是,如果您打開了一個應用程序的多個副本 - 它們具有相同的標題/文本 - 您可以在使用句柄時唯一地標識它們。 當您為標題參數使用窗口句柄時,文本參數將被完全忽略。

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

今天我找到了VBA Express 發布的解決方案。 它將列出活動表上所有打開的窗口。

請注意,“一個窗口”不一定是用戶通常想到的……有些窗口是許多窗口的集合,Windows 是這樣看待它的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM