|
添加引用无非有两种方法,第一种是使用addfromfile 另外一种是使用addfromguid方法。 这两种方法的优缺点如下:
addfromguid方法可以避免判断应用程序的版本。 例如 Microsoft Excel's object model 的GUID 是 {00020813-0000-0000-C000-000000000046} 。无论是2007 2003还是2010 ,这个GUID是不会变化的。这时使用addfromguid方法避免了要对不同版本进行判断。就算用户将来使用2012 (地球末日版),也不需要修改VBA代码。
addfromfile 方法用在加载用户自定义组件,或者大型应用程序的某一个功能模块是比较有用。 在发布时,只需要拷贝相应的组件文件放在插件目录下,然后使用VBA动态获得插件所在目录就可以加载了。这样做的好处是显而易见的。 第一,你不用为了使用某一个程序的其中一个功能而安装整个程序。只需要复制其中一个组件就可以了。 第二,发布和更新也比较灵活, 插件可以放在任何地方。 更新时可以动态unload插件,然后使用vba下载更新后的组件并且覆盖旧组件。
以上两种方法只要综合利用一定能够使插件的升级更新变得简便。
另外附上两段代码:
Sub Grab_References()
Dim n As Integer
On Error Resume Next
For n = 1 To ThisWorkbook.VBProject.References.count
Cells(n, 1) = ThisWorkbook.VBProject.References.Item(n).Name
Cells(n, 2) = ThisWorkbook.VBProject.References.Item(n).Description
Cells(n, 3) = ThisWorkbook.VBProject.References.Item(n).GUID
Cells(n, 4) = ThisWorkbook.VBProject.References.Item(n).Major
Cells(n, 5) = ThisWorkbook.VBProject.References.Item(n).Minor
Cells(n, 6) = ThisWorkbook.VBProject.References.Item(n).fullpath
Next n
End Sub
这段代码可以获取当前文档所有引用的详细信息
Visual Basic For Applications {000204EF-0000-0000-C000-000000000046} 4 0 C:\Program Files\Common Files\Microsoft Shared\VBA\VBA6\VBE6.DLL
Microsoft Excel 12.0 Object Library {00020813-0000-0000-C000-000000000046} 1 6 C:\Program Files\Microsoft Office\Office12\EXCEL.EXE
Microsoft Forms 2.0 Object Library {0D452EE1-E08F-101A-852E-02608C4D0BB4} 2 0 C:\WINDOWS\system32\FM20.DLL
OLE Automation {00020430-0000-0000-C000-000000000046} 2 0 C:\WINDOWS\system32\stdole2.tlb
Microsoft Office 12.0 Object Library {2DF8D04C-5BFA-101B-BDE5-00AA0044DE52} 2 4 C:\Program Files\Common Files\Microsoft Shared\office12\mso.dll
Microsoft Scripting Runtime {420B2830-E718-11CF-893D-00A0C9054228} 1 0 C:\WINDOWS\system32\scrrun.dll
Microsoft ActiveX Data Objects 2.8 Library {2A75196C-D9EB-4129-B803-931327F72D5C} 2 8 C:\Program Files\Common Files\System\ado\msado15.dll
第二段代码:使用addfromguid引用。
Sub AddReference()
Dim RefItem(7, 3) As Variant
RefItem(0, 0) = "{000204EF-0000-0000-C000-000000000046}"
RefItem(0, 1) = 4
RefItem(0, 2) = 0
RefItem(1, 0) = "{00020813-0000-0000-C000-000000000046}"
RefItem(1, 1) = 1
RefItem(1, 2) = 6
RefItem(2, 0) = "{0D452EE1-E08F-101A-852E-02608C4D0BB4}"
RefItem(2, 1) = 2
RefItem(2, 2) = 0
RefItem(3, 0) = "{00020430-0000-0000-C000-000000000046}"
RefItem(3, 1) = 2
RefItem(3, 2) = 0
RefItem(4, 0) = "{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}"
RefItem(4, 1) = 2
RefItem(4, 2) = 4
RefItem(5, 0) = "{420B2830-E718-11CF-893D-00A0C9054228}"
RefItem(5, 1) = 1
RefItem(5, 2) = 0
RefItem(6, 0) = "{2A75196C-D9EB-4129-B803-931327F72D5C}"
RefItem(6, 1) = 2
RefItem(6, 2) = 8
Dim strGUID As String, theRef As Variant, I As Long
On Error Resume Next
For I = ThisWorkbook.VBProject.References.count To 1 Step -1
Set theRef = ThisWorkbook.VBProject.References.Item(I)
If theRef.isbroken = True Then
ThisWorkbook.VBProject.References.Remove theRef
End If
Next I
err.clear
Dim errmsg As String
For I = 0 To 6
ThisWorkbook.VBProject.References.AddFromGuid GUID:=RefItem(I, 0), Major:=RefItem(I, 1), Minor:=RefItem(I, 2)
Select Case err.Number
Case Is = 32813
'引用已经加载,无需做任何事情
Case Is = vbNullString
'成功加载
Case Else
'加载出现错误,保存错误信息
errmsg = errmsg & RefItem(I, 0) & "出现错误错误"
End Select
Next I
If errmsg <> "" Then
MsgBox errmsg
End If
On Error GoTo 0
End Sub
[ 本帖最后由 citypanther 于 2011-6-9 02:29 编辑 ] |
评分
-
2
查看全部评分
-
|