|
'VB编写的DLL文件的注册方式
'1.方式一:在工具-引用-浏览 中加载DLL
'2.方式二:使用自定义函数把DLL加载到引用中
'事件,关闭工作簿
Private Sub Workbook_BeforeClose(Cancel As Boolean)
'Shell "Regsvr32 /u /s " & Chr(34) & ThisWorkbook.Path & "\msppmd5.dll" & Chr(34)
ThisWorkbook.VBProject.References.Remove ThisWorkbook.VBProject.References("COMMD5Lib") '将DLL文件从引用中取消
End Sub
'事件,文档打开时加载
Private Sub Workbook_Open()
On Error GoTo errline
'自动引用dll
'Shell "Regsvr32 /s " & Chr(34) & ThisWorkbook.Path & "\msppmd5.dll" & Chr(34)
'ThisWorkbook.VBProject.References.AddFromFile (ThisWorkbook.Path & "\msppmd5.dll") '将DLL文件增加到引用中(简单加载)
Call CheckRegDll '首先检查DLL有没有引用,没有则添加到引用中(经检查后加载)
Exit Sub
errline:
MsgBox "程序在注册DLL函数时出现错误!"
End Sub
'显示当前所有引用
Sub ShowRefs()
'先手工把需要添加到引用中的文件,加到引用中。再使用这个函数,找到这个DLL文件的(GUID:="{B8C30799-1B55-11D2-BED2-00C04FB6FA0D}", Major:=1, Minor:=0),写入CheckRegDll()函数的相应位置
Dim rf
'遍历所有已使用的引用
For Each rf In ThisWorkbook.VBProject.References
Debug.Print "Name:=" & rf.Name, ", GUID:=""" & rf.GUID, ", Major:=" & rf.Major, ", Minor:=" & rf.Minor, ", FullPath:=" & rf.FullPath
Next
End Sub
'首先检查DLL有没有引用,没有则添加到引用中
Public Sub CheckRegDll()
Dim bolFindAdo As Boolean, refed
On err GoTo ERRHandle
For Each refed In ThisWorkbook.VBProject.References
If refed.Name = "COMMD5Lib" Then '检查DLL有没有引用
If refed.isbroken Then
ThisWorkbook.VBProject.References.Remove refed '如引用已损坏,删除
Else
bolFindAdo = True
Exit For
End If
End If
Next
If bolFindAdo = False Then
'还没安装,现在安装
ThisWorkbook.VBProject.References.AddFromGuid _
GUID:="{B8C30799-1B55-11D2-BED2-00C04FB6FA0D}", Major:=1, Minor:=0
End If
Exit Sub
ERRHandle:
MsgBox (err.Description & "加载失败,请联系管理员!")
Exit Sub
End Sub
|
|