ゆっくり開発

思いついた便利コードを気ままにアップしていきます。公開しているソースコードはすべてMITライセンスです。

【VBA】参照設定の追加

Private Const 参照設定VBA           As String = "C:\Program Files\Common Files\Microsoft Shared\VBA\VBA7.1\VBE7.DLL"
Private Const 参照設定Excel         As String = "C:\Program Files\Microsoft Office\root\Office16\EXCEL.EXE"
Private Const 参照設定OLE           As String = "C:\Windows\System32\stdole2.tlb"
Private Const 参照設定OfficeLibrary As String = "C:\Program Files\Common Files\Microsoft Shared\OFFICE16\MSO.DLL"
Private Const 参照設定ADODB         As String = "C:\Program Files\Common Files\System\ado\msado15.dll"
Private Const 参照設定Regular       As String = "C:\Windows\System32\vbscript.dll\3"
Private Const 参照設定Scripting     As String = "C:\Windows\System32\scrrun.dll"
Sub 参照設定一覧表示()

    On Error GoTo ■エラー処理
    
    Dim 設定済の内容 As Object
    
    For Each 設定済の内容 In このブック.VBProject.References
        With 設定済の内容
            Debug.Print _
                "名称" & vbTab & vbTab & ":" & .Name & vbCrLf & _
                "参照設定名" & vbTab & ":" & .Description & vbCrLf & _
                "フルパス" & vbTab & ":" & .FullPath & vbCrLf & _
                "------------------------"
        End With
    Next
    
    Debug.Print "●参照設定一覧表示の成功●"
    GoTo ■終了処理
    
■エラー処理:
    If Err.Number = 1004 Then
        MsgBox "「Excelのオプション」" & vbCrLf _
             & "→「マクロの設定」" & vbCrLf _
             & "→「開発者向けのマクロ設定」" & vbCrLf _
             & "→「VBA プロジェクト オブジェクト モデルへのアクセスを信頼する」にチェックを入れてください。" _
             , , "Excelの設定変更が必要です。"
    Else
        Debug.Print "▲参照設定一覧表示のエラー▲" & vbCrLf & vbTab & Err.Number & vbTab & Err.Description
    End If
    
■終了処理:

End Sub
Private Function 参照設定あり(ByRef 確認対象フルパス As String) As Boolean
    
    On Error GoTo ■エラー処理
    
    Dim 設定済の内容 As Object
    
    参照設定あり = False
    
    For Each 設定済の内容 In このブック.VBProject.References
        If 設定済の内容.FullPath = 確認対象フルパス Then
            参照設定あり = True
            Exit For
        End If
    Next
    
    GoTo ■終了処理
    
■エラー処理:
    If Err.Number = 1004 Then
        MsgBox "「Excelのオプション」" & vbCrLf _
             & "→「マクロの設定」" & vbCrLf _
             & "→「開発者向けのマクロ設定」" & vbCrLf _
             & "→「VBA プロジェクト オブジェクト モデルへのアクセスを信頼する」にチェックを入れてください。" _
             , , "Excelの設定変更が必要です。"
    Else
        Debug.Print "▲参照設定ありのエラー▲" & vbCrLf & vbTab & Err.Number & vbTab & Err.Description
    End If
    
■終了処理:

End Function
Sub 参照設定追加処理()
    
    On Error GoTo ■エラー処理
    
    With ActiveWorkbook.VBProject.References
        If 参照設定あり(参照設定VBA) = False Then .AddFromFile 参照設定VBA
        If 参照設定あり(参照設定Excel) = False Then .AddFromFile 参照設定Excel
        If 参照設定あり(参照設定OLE) = False Then .AddFromFile 参照設定OLE
        If 参照設定あり(参照設定OfficeLibrary) = False Then .AddFromFile 参照設定OfficeLibrary
        If 参照設定あり(参照設定ADODB) = False Then .AddFromFile 参照設定ADODB
        If 参照設定あり(参照設定Regular) = False Then .AddFromFile 参照設定Regular
        If 参照設定あり(参照設定Scripting) = False Then .AddFromFile 参照設定Scripting
    End With
    
    参照設定一覧表示
    
    Debug.Print "●参照設定追加処理の成功●"
    GoTo ■終了処理
    
■エラー処理:
    Debug.Print "▲参照設定追加処理のエラー▲" & vbCrLf & vbTab & Err.Number & vbTab & Err.Description
    
■終了処理:

End Sub