迭代未注册的加载项(.xla)

我需要帮助

  • 弄清楚如何使用Tools > Add-ins菜单path来遍历尚未在Excel中注册的当前打开的Excel加载项文件(.xla)
  • 更具体地说,我感兴趣的任何工作簿不会出现在加载项对话框中,但具有ThisWorkbook.IsAddin = True

certificate这个问题:

尝试按如下方式循环浏览工作簿时,不会获得带有.AddIn = True工作簿:

 Dim book As Excel.Workbook For Each book In Application.Workbooks Debug.Print book.Name Next book 

通过加载项循环不会获得未注册的加载项:

 Dim addin As Excel.AddIn For Each addin In Application.AddIns Debug.Print addin.Name Next addin 

通过VBProjects集合循环工作,但只有当用户在macros安全设置中特别可信地访问Visual Basic项目 – 这很less:

 Dim vbproj As Object For Each vbproj In Application.VBE.VBProjects Debug.Print vbproj.Filename Next vbproj 

但是,如果工作簿的名称是已知的,则可以直接引用工作簿,而不pipe它是否是加载项:

 Dim book As Excel.Workbook Set book = Application.Workbooks("add-in.xla") 

但是,如果不知道名称,用户的macros安全设置是不可靠的,那么如何获得对此工作簿的引用呢?

从Office 2010开始,有一个与.AddIns相同的新集合.AddIns2,但也包含未注册的.XLA插件。

 Dim a As AddIn Dim w As Workbook On Error Resume Next With Application For Each a In .AddIns2 If LCase(Right(a.name, 4)) = ".xla" Then Set w = Nothing Set w = .Workbooks(a.name) If w Is Nothing Then Set w = .Workbooks.Open(a.FullName) End If End If Next End With 

我有安装(和在VBE)不能通过Exel 2013用户的Addin (在工作环境中)的Addin问题。

修改克里斯C 的解决scheme给了一个很好的解决方法。

 Dim a As AddIn Dim wb As Workbook On Error Resume Next With Application .DisplayAlerts = False For Each a In .AddIns2 Debug.Print a.Name, a.Installed If LCase(Right$(a.Name, 4)) = ".xla" Or LCase(Right$(a.Name, 5)) Like ".xla*" Then Set wb = Nothing Set wb = .Workbooks(a.Name) wb.Close False Set wb = .Workbooks.Open(a.FullName) End If Next .DisplayAlerts = True End With 

我仍然在寻找这个问题的一个理智的解决scheme,但目前看来,读取所有工作簿窗口的窗口文本给出了所有打开的工作簿,加载或不加载的集合:

 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 

那这个呢:

 Public Sub ListAddins() Dim ai As AddIn For Each ai In Application.AddIns If Not ai.Installed Then Debug.Print ai.Application, ai.Parent, ai.Name, ai.FullName End If Next End Sub 

任何使用?

使用= DOCUMENTS,一个Excel4macros函数。

 Dim Docs As Variant Docs = Application.Evaluate("documents(2)") 

这里是它的文档( 在这里可用):

DOCUMENTS
以文本forms的水平数组返回按字母顺序排列的指定打开工作簿的名称。 使用DOCUMENTS检索打开工作簿的名称,以便在操作打开的工作簿的其他函数中使用。

句法
文件(type_num,match_text)
Type_num是一个数字,指定是否在工作簿数组中包含附加工作簿,根据下表。

 Type_num Returns 1 or omitted Names of all open workbooks except add-in workbooks 2 Names of add-in workbooks only 3 Names of all open workbooks 

Match_text指定要返回其名称并可包含通配符的工作簿。 如果省略match_text,则DOCUMENTS将返回所有打开的工作簿的名称。

迭代通过registry的可能性? 我知道这并不能给你一个你正在使用的Excel实例的快照,但是一个新的实例将会使用什么 – 但是根据你所需要的,它可能已经足够了。

相关的键是:

 'Active add-ins are in values called OPEN* HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Excel\Options 'Inactive add-ins are in values of their full path HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Excel\Add-in Manager