下面是我专门用于处理引用修复问题的两个代码,你可以参考参考.其中有你要的所有关键代码.
其中,我是把所有的引用的信息建在一张表中的.有新的引用时,自动存入表中.引用损坏时自动修复.
Function xiufuyinYong()
On Error Resume Next
Dim fs '用于操作文件及文件目录
Set fs = CreateObject("Scripting.FileSystemObject")
'检查前台文件目录是否存在,如果不存在,则创建之
fs.CreateFolder CurrentProject.path & "\dll\adodb"
'复制文件
'fs.copyfile fileNM(3), BackupFileNm(3)
Dim StrSQL As String
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
StrSQL = "SELECT * FROM table_Application_References;"
rs.Open StrSQL, CurrentProject.Connection, adOpenKeyset, adLockOptimistic
rs.MoveFirst
Do Until rs.EOF
Debug.Print rs.Fields("FullPath")
On Error Resume Next
fs.CopyFile rs.Fields("FullPath"), CurrentProject.path & "\dll"
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
If Err.Number > 0 Then
Debug.Print Err.Description
'Rs.Close
'Set Rs = Nothing
End If
End Function
Function Yinyong() '检查新的引用,将新加入的引用加入到表中,将表中的引用加入到系统中.
On Error Resume Next
Dim i As Integer
Dim strGuid As String
Dim newGuid As String
Dim StrSQL As String
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
StrSQL = "SELECT * FROM table_Application_References;"
rs.Open StrSQL, CurrentProject.Connection, adOpenKeyset, adLockOptimistic
For i = 1 To Application.References.Count
'Debug.Print i
If Application.References(i).IsBroken = False Then
'Debug.Print DLookup("guid", "table_Application_References", "guid='" & Application.References(i).Guid & "'")
If IsNull(DLookup("guid", "table_Application_References", "guid='" & Application.References(i).Guid & "'")) Then
With rs
rs.AddNew
!Name = Application.References(i).Name '引用的名称
!FullPath = Application.References(i).FullPath '引用的路径
!Major = Application.References(i).Major '引用的版本号
!Guid = Application.References(i).Guid '引用的ID号
!Minor = Application.References(i).Minor '引用的版次号
.Update
End With
Debug.Print "已经将新的引用:'" & Application.References(i).Name & "'加入到表中."
End If
End If
Next i
With rs
rs.MoveFirst
Do Until rs.EOF
'Debug.Print rs.Fields("name")
strGuid = rs.Fields("guid")
newGuid = ""
For i = 1 To Application.References.Count
If Application.References(i).Guid = strGuid Then
'Application.References.Count
'Debug.Print strGuid
newGuid = strGuid
'Debug.Print newGuid
Exit For
Else
newGuid = ""
End If
Next i
If newGuid = "" Then
Debug.Print "引用中未修复的引用:" & rs.Fields("name")
Application.References.AddFromFile rs.Fields("fullpath")
Debug.Print "修复的引用:" & rs.Fields("fullpath")
End If
rs.MoveNext
Loop
End With
rs.Close
Set rs = Nothing
If Err.Number <> 0 Then Debug.Print Err.Description
'Application.References.AddFromFile "C:\Windows\system32\SHAPE.OCX"
' Application.References.AddFromFile "C:\Windows\system32\wmp.dll" '增加一个新的引用
' Application.References.AddFromFile "D:\MDB\tt\技术管理信息化系统V252 - 单机版 - 复制-2009-1-18--12.MDB" '增加一个新的引用
' Application.References.Remove "D:\MDB\tt\技术管理信息化系统V252 - 单机版 - 复制-2009-1-18--12.MDB" '增加一个新的引用
'Dim yy
'Set yy = Application.References!ACCESS9 '删除一个MDB文件的引用
'References.Remove yy
'RemoveReference = True
End Function