Iterating unregistered add-ins (.xla)

后端 未结 6 1582
灰色年华
灰色年华 2020-12-31 13:17

I need help in

  • figuring out how to iterate through currently open Excel add-in files (.xla) that have not been registered in Excel using the Too
6条回答
  •  伪装坚强ぢ
    2020-12-31 13:44

    I'm still on the lookout for a sane solution for this problem, but for the time being it seems that reading the window texts of all workbook windows gives a collection of all open workbooks, add-in or not:

    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
    Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    
    Public Function GetAllOpenWorkbooks() As Collection
    
    'Retrieves a collection of all open workbooks and add-ins.
    
    Const EXCEL_APPLICATION_WINDOW  As String = "XLDESK"
    Const EXCEL_WORKBOOK_WINDOW     As String = "EXCEL7"
    
    Dim hWnd                As Long
    Dim hWndExcel           As Long
    Dim contentLength       As Long
    Dim buffer              As String
    Dim bookName            As String
    Dim books               As Collection
    
    Set books = New Collection
    
    'Find the main Excel window
    hWndExcel = FindWindowEx(Application.hWnd, 0&, EXCEL_APPLICATION_WINDOW, vbNullString)
    
    Do
    
        'Find next window
        hWnd = FindWindowEx(hWndExcel, hWnd, vbNullString, vbNullString)
    
        If hWnd Then
    
            'Create a string buffer for 100 chars
            buffer = String$(100, Chr$(0))
    
            'Get the window class name
            contentLength = GetClassName(hWnd, buffer, 100)
    
            'If the window found is a workbook window
            If Left$(buffer, contentLength) = EXCEL_WORKBOOK_WINDOW Then
    
                'Recreate the buffer
                buffer = String$(100, Chr$(0))
    
                'Get the window text
                contentLength = GetWindowText(hWnd, buffer, 100)
    
                'If the window text was returned, get the workbook and add it to the collection
                If contentLength Then
                    bookName = Left$(buffer, contentLength)
                    books.Add Excel.Application.Workbooks(bookName), bookName
                End If
    
            End If
    
        End If
    
    Loop While hWnd
    
    'Return the collection
    Set GetAllOpenWorkbooks = books
    
    End Function
    

提交回复
热议问题