10

助けが必要です

  • メニュー パスを使用して、Excel に登録されていない、現在開いている Excel アドイン ファイル(.xla)を反復処理する方法を見つけます。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")

しかし、名前がわからず、ユーザーのマクロ セキュリティ設定が信頼できない場合、このブックへの参照を取得するにはどうすればよいでしょうか。

4

6 に答える 6

11

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
于 2013-01-17T20:30:37.643 に答える
1

インストールされている (および VBE にある) アドインが、AddinExel 2013 のユーザー経由で (作業環境で) 利用できないという問題がありました。

Chris Cのソリューションをいじってみると、適切な回避策が得られました。

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
于 2016-09-21T04:37:46.700 に答える
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
于 2008-11-13T16:20:02.263 に答える
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

使い道は?

于 2008-11-26T11:58:57.887 に答える
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
于 2008-12-12T14:16:51.320 に答える
0

Excel4 のマクロ関数である =DOCUMENTS を使用します。

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

そのためのドキュメントは次のとおりです(ここから入手できます):

DOCUMENTS
テキスト形式の水平配列として、指定された開いているワークブックの名前をアルファベット順に返します。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 は開いているすべてのワークブックの名前を返します。

于 2008-11-28T13:40:01.850 に答える