Sub FileSystemObject_Sample1()
Dim obj As Scripting.FileSystemObject
Set obj = New Scripting.FileSystemObject
Call obj.CreateFolder("C:\\aaa")
Set obj = Nothing
End Sub
ちょっと、うるさいので、あっさりとこう書くこともできます・・・②
Sub FileSystemObject_Sample1()
Dim obj As New Scripting.FileSystemObject
Call obj.CreateFolder("C:\\aaa")
Set obj = Nothing
End Sub
「 obj 」や「 Nothing 」も削って、こう書くことも・・やりすぎですかね?w・・・③
Sub FileSystemObject_Sample1()
With New Scripting.FileSystemObject
Call .CreateFolder("C:\\aaa")
End With
End Sub
Sub FileSystemObject_Sample2()
Dim obj As Object
Set obj = CreateObject("Scripting.FileSystemObject")
Call obj.CreateFolder("C:\\aaa")
Set obj = Nothing
End Sub
Sub CallReferencesAddFromFile_Sample()
' 参照に追加するライブラリのファイルパスを用意します(ここではFileSystemObjectを追加する例)
Const DLL_SCRRUN As String = "C:\Windows\SysWOW64\scrrun.dll"
' 参照追加関数にライブラリのファイルパスを渡して呼び出す
Call References_AddFromFile(DLL_SCRRUN)
End Sub
②参照を追加する関数
Sub References_AddFromFile(dllPath As String)
' 参照を追加する
Call Excel.ThisWorkbook.VBProject.References.AddFromFile(dllPath)
End Sub
・参照をVBAで削除
参照を削除するには、以下のように行います
③参照削除の呼び出し元関数
Sub CallReferencesRemove_Sample()
' 参照から削除するライブラリのファイルパスを用意します(ここではFileSystemObjectを削除する例)
Const DLL_SCRRUN As String = "C:\Windows\SysWOW64\scrrun.dll"
' 削除関数にライブラリのファイルパスを渡して呼び出す
Call References_Remove(DLL_SCRRUN)
End Sub
④参照を削除する関数
Sub References_Remove(dllPath As String)
Dim obj As Object
With Excel.ThisWorkbook.VBProject
' ファイルで参照しているものを検索します
For Each obj In .References
' 参照不可ではない場合のみ実施する
If (obj.IsBroken = False) Then
' Name、Description、FullPath、IsBrokenのプロパティを持っています
If (obj.FullPath = dllPath) Then
' 参照を削除する
Call .References.Remove(obj)
End If
End If
Next
End With
End Sub
Sub References_Remove2(dllPath As String)
Dim refers As VBIDE.References
Dim ref As VBIDE.Reference
Dim rName As String
Set refers = Excel.ThisWorkbook.VBProject.References
' 参照しているものを探します
For Each ref In refers
' 参照不可ではない場合のみ実施する
If (ref.IsBroken = False) Then
' ライブラリのファイルパスで比較
If (ref.FullPath = dllPath) Then
rName = ref.Name
Exit For
End If
End If
Next
If (Len(rName) > 0) Then
' Nameプロパティを渡してオブジェクトを取得する
Set ref = refers.Item(rName)
' 参照を削除する
Call refers.Remove(ref)
Set ref = Nothing
End If
Set refers = Nothing
End Sub
⑧ライブラリ名で参照を削除する呼び出し元関数
Sub CallReferencesRemove_Sample3()
' 参照から削除するライブラリ名
Const DLL_SCRRUN As String = "Scripting"
' 削除関数にライブラリ名を渡して呼び出す
Call References_Remove3(DLL_SCRRUN)
End Sub
⑨ライブラリ名で参照を削除する関数
Sub References_Remove3(dllName As String)
Dim ref As VBIDE.Reference
With Excel.ThisWorkbook.VBProject
Set ref = .References(dllName)
If Not (ref Is Nothing) Then
Call .References.Remove(ref)
End If
Set ref = Nothing
End With
End Sub
パッと見は綺麗になったんですが、 IsBroken プロパティを見ていないし、 「 Set ref = .References(dllName) 」でライブラリがなかったらエラーになります!
Sub CallReferencesAddFromGuid_Sample()
' 参照に追加するライブラリのGUIDを用意します(ここではFileSystemObjectを追加する例)
Const DLL_SCRRUN As String = "{420B2830-E718-11CF-893D-00A0C9054228}"
' 参照追加関数にライブラリのGUIDとバージョン(MajorとMinor)を渡して呼び出す
Call References_AddFromGuid(DLL_SCRRUN, 1, 0)
End Sub
⑦ GUID を使用して参照を追加する関数
Sub References_AddFromGuid(guid As String, major As Long, minor As Long)
' 参照を追加する
Call Excel.ThisWorkbook.VBProject.References.AddFromGuid(guid, major, minor)
End Sub
・参照設定の一覧表示
VBA で参照しているライブラリの一覧を表示してみます
Sub GetReferencesList_Sample()
Dim refers As VBIDE.References
Dim ref As VBIDE.Reference
Set refers = Excel.ThisWorkbook.VBProject.References
For Each ref In refers
Debug.Print "--------------------------------------------------------------------------------------------------------------"
Debug.Print ref.Name, ref.guid & " : " & ref.Major & "." & ref.Minor & " : " & ref.Description
Debug.Print ref.FullPath
Next ref
Debug.Print "--------------------------------------------------------------------------------------------------------------"
出力結果
--------------------------------------------------------------------------------------------------------------
VBA {000204EF-0000-0000-C000-000000000046} : 4.2 : Visual Basic For Applications
C:\Program Files (x86)\Common Files\Microsoft Shared\VBA\VBA7.1\VBE7.DLL
--------------------------------------------------------------------------------------------------------------
Excel {00020813-0000-0000-C000-000000000046} : 1.9 : Microsoft Excel 16.0 Object Library
C:\Program Files (x86)\Microsoft Office\Root\Office16\EXCEL.EXE
--------------------------------------------------------------------------------------------------------------
stdole {00020430-0000-0000-C000-000000000046} : 2.0 : OLE Automation
C:\Windows\SysWOW64\stdole2.tlb
--------------------------------------------------------------------------------------------------------------
Office {2DF8D04C-5BFA-101B-BDE5-00AA0044DE52} : 2.8 : Microsoft Office 16.0 Object Library
C:\Program Files (x86)\Common Files\Microsoft Shared\OFFICE16\MSO.DLL
--------------------------------------------------------------------------------------------------------------
Outlook {00062FFF-0000-0000-C000-000000000046} : 9.6 : Microsoft Outlook 16.0 Object Library
C:\Program Files (x86)\Microsoft Office\Root\Office16\MSOUTL.OLB
--------------------------------------------------------------------------------------------------------------
VBIDE {0002E157-0000-0000-C000-000000000046} : 5.3 : Microsoft Visual Basic for Applications Extensibility 5.3
C:\Program Files (x86)\Common Files\Microsoft Shared\VBA\VBA6\VBE6EXT.OLB
--------------------------------------------------------------------------------------------------------------
Scripting {420B2830-E718-11CF-893D-00A0C9054228} : 1.0 : Microsoft Scripting Runtime
C:\Windows\SysWOW64\scrrun.dll
--------------------------------------------------------------------------------------------------------------