我具有以下将Microsoft Script Runtime
引用自动添加到引用列表的功能。但是,如果用户已经包含Microsoft script runtime
,则会显示error Name conflicts with existing module,project, object library
。
Microsoft script runtime
如果参考中未包含自动添加的条件,如何设置条件;如果已经添加了自动添加的条件,则不执行任何操作?
Private Function AddScriptingLibrary() As Boolean
Const GUID As String = "{420B2830-E718-11CF-893D-00A0C9054228}"
On Error GoTo errHandler
ThisWorkbook.VBProject.References.AddFromGuid GUID, 1, 0
AddScriptingLibrary = True
Exit Function
errHandler:
MsgBox Err.Description
End Function
您需要先枚举项目的引用,以便检查引用是否已经存在。
我已经添加了对Microsoft Visual Basic for Applications Extensibility 5.3的引用
Option Explicit
Function AddScriptingLibrary()
Const GUID_Scripting = "{420B2830-E718-11CF-893D-00A0C9054228}"
Dim proj As VBIDE.VBProject
Dim ref As VBIDE.Reference
Dim ScriptingLibraryIsReferenced As Boolean
Set proj = ThisWorkbook.VBProject
For Each ref In proj.References
If ref.GUID = GUID_Scripting Then
ScriptingLibraryIsReferenced = True
AddScriptingLibrary = True
Exit Function
End If
Next ref
If Not ScriptingLibraryIsReferenced Then
On Error GoTo errHandler
proj.References.AddFromGuid GUID_Scripting, 1, 0
AddScriptingLibrary = True
Exit Function
errHandler:
MsgBox Err.Description
End If
End Function
EDIT的功能相同,但是没有早期绑定参考Visual Basic For Applications Extensibility 5.3参考:
Option Explicit
Function AddScriptingLibrary()
Const GUID_Scripting = "{420B2830-E718-11CF-893D-00A0C9054228}"
Dim proj As Object 'VBIDE.VBProject
Dim ref As Object 'VBIDE.Reference
Dim ScriptingLibraryIsReferenced As Boolean
Set proj = ThisWorkbook.VBProject
For Each ref In proj.References
If ref.GUID = GUID_Scripting Then
ScriptingLibraryIsReferenced = True
AddScriptingLibrary = True
Exit Function
End If
Next ref
If Not ScriptingLibraryIsReferenced Then
On Error GoTo errHandler
proj.References.AddFromGuid GUID_Scripting, 1, 0
AddScriptingLibrary = True
Exit Function
errHandler:
MsgBox Err.Description
End If
End Function
但是,如果您对后期绑定代码的缺点感到满意,则甚至不需要引用Scripting.Runtime
,因为您可以使用:
Option Explicit
Sub PrintDriveCount()
Dim FSO As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
'Print the number of drives in the FileSystemObject
Debug.Print FSO.Drives.Count
End Function
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句