2008-11-13 110 views
8

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

  • 帮助搞清楚如何通过当前打开的Excel插件文件迭代(.xla)使用还没有被注册在Excel中Tools > Add-ins菜单路径。
  • 更具体地说,我对没有出现在加载项对话框中但有ThisWorkbook.IsAddin = True的任何工作簿感兴趣。

演示该问题:

通过工作簿试图循环如下与.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集合循环工作,但只有当用户具有特定信任访问Visual Basic项目中的宏安全设置 - 这是很少:

Dim vbproj As Object 

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

但是,如果工作簿的名称是已知的,工作簿可以直接不管它是一个加载项中引用或不:

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

但到底如何让参照本工作簿,如果名字不知道,和用户的宏安全设置不能依靠?

回答

7

随着Office 2010中,有一个新的集合.AddIns2这是一样的.AddIns而且还包括未注册的.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 
+1

谢谢!这在4年前会非常有用:P接受你的回答,因为我接受的答案太过于粗鲁。 – jevakallio 2013-01-17 20:49:18

0

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

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 
0

这个怎么样:

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 

有什么用处?

0

使用= DOCUMENTS,一个Excel4宏函数。

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

下面是它的文档(可here):

DOCUMENTS
返回,如文本形式的横向排列,按字母顺序指定打开的工作簿的名称。使用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将返回所有打开的工作簿的名称。

+0

不幸的是,这一功能不再在Excel的现代版本的作品(2010年至少,虽然我觉得微软至少Excel 2007中移除Excel4功能的支持)。 – GlennFromIowa 2017-04-17 17:39:08

0

迭代通过注册表的可能性?我知道这并不能给你一个你正在使用的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 
1

我曾与安装(并在VBE)加载项的问题不是通过对Exel的2013用户的Addin可(在工作环境中)。

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

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