Access自動刪除引用併自動重新添加引用的函數-判斷瞭32位及64位Windows和Office的多種情況

2017-07-23 19:46:00
zstmtony
原創
1594

Access自動刪除指定的引用,然後再自動重新添加這箇引用的通用代碼-判斷瞭32位及64位Windows和32位Office及64位Office的多種情況

在編寫Access通用平颱或通用開髮庫時,或使用第三方控件或鏈接庫時,由於這箇開髮庫可能經常會更新,由於更新後,開髮庫的對象和屬性和方法都有所改變

必鬚要手工對這箇對象重新引用,如果一次兩次還可以手工操作,如果經常更新,就需要寫一箇通用刪除和添加引用的函數瞭


自動重新添加Ocx dll 等控件或鏈接庫的引用(DevlibOcx引用)


Option Compare Database
Option Explicit

Private Const POINTERSIZE As Long = 4
Private Const ZEROPOINTER As Long = 0

Const MAX_LEN = 200    '字符串最大長度
Private Declare Sub apiCopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)

Private Declare Function GetSystemWow64Directory Lib "kernel32" Alias "GetSystemWow64DirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long

Private Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long


'Access或Excel VBA或VB6判斷windows繫統是32位還是64位
'來判斷繫統是32bit還是64bit,主要通過API來實現,先在窗體模塊裡申明以下API定義:
Private Declare Function GetProcAddress Lib "kernel32" (ByVal hModule As Long, ByVal lpProcName As String) As Long
Private Declare Function GetModuleHandle Lib "kernel32" Alias "GetModuleHandleA" (ByVal lpModuleName As String) As Long
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Declare Function IsWow64Process Lib "kernel32" (ByVal hProc As Long, bWow64Process As Boolean) As Long

'然後在窗體或模塊中再建立一箇判斷繫統是32位還是64位的函數,返迴值是佈爾值,如果繫統是32位,此函數返迴值是Flase 如果是64位,返迴值是True,函數代碼如下:
Public Function Is64bit() As Boolean
    Dim handle As Long, bolFunc As Boolean
    bolFunc = False
    handle = GetProcAddress(GetModuleHandle("kernel32"), "IsWow64Process")
    If handle > 0 Then
        IsWow64Process GetCurrentProcess(), bolFunc
    End If
    Is64bit = bolFunc
End Function

'穫取64位 windows及32位windows繫統 的繫統目録 ,正常都是System32
Public Function GetSystemDir() As String
    Dim sTmp As String * MAX_LEN  '存放結果的固定長度的字符串
    Dim nLength As Long  '字符串的實際長度
    Dim length As Long
 
    Dim strPath As String
     '穫得System目録
        length = GetSystemDirectory(sTmp, MAX_LEN)
        strPath = Left(sTmp, length)
      GetSystemDir = strPath
End Function
 
'穫取64位windows繫統下32位的繫統目録 ,正常是SysWow64
Public Function GetSystemWow64Dir() As String
    Dim sTmp As String * MAX_LEN  '存放結果的固定長度的字符串
    Dim nLength As Long  '字符串的實際長度
    Dim length As Long
 
    Dim strPath As String
     '穫得System目録
        length = GetSystemWow64Directory(sTmp, MAX_LEN)
        strPath = Left(sTmp, length)
      GetSystemWow64Dir = strPath
      
End Function



'自動重新添加Ocx dll 等控件或鏈接庫的引用(DevlibOcx引用)
Public Function ReAddDevLibReference()
    Dim ref As Reference
    Dim strMDE As String
    On Error Resume Next
    strMDE = CurrentDb.Properties("MDE")
    On Error GoTo 0
    If strMDE = "" Then
 
#If Win64 Then
        If Dir(GetSystemDir & "\DevLibOcx.dll") = "" Then
                MsgBox GetSystemDir & "\DevLibOcx.dll 文件不存在,請檢查文件是否丟失!"
        End If
#Else
    If Is64bit Then
        If Dir(GetSystemWow64Dir & "\DevLibOcx.dll") = "" Then
                MsgBox GetSystemWow64Dir & "\DevLibOcx.dll 文件不存在,請檢查文件是否丟失!"
        End If
    Else
        If Dir(GetSystemDir & "\DevLibOcx.dll") = "" Then
                MsgBox GetSystemDir & "\DevLibOcx.dll 文件不存在,請檢查文件是否丟失!"
        End If
    End If
#End If
        
        For Each ref In Application.References
            If ref.FullPath Like "*DevLibOcx.dll" Then
               Application.References.Remove ref
               Exit For
            End If
        Next
    
#If Win64 Then ’ 64bit Office or  32bit Office  Application.References.AddFromFile GetSystemDir & "\DevLibOcx.dll"
#Else
       If Is64bit Then
        Application.References.AddFromFile GetSystemWow64Dir & "\DevLibOcx.dll"
       Else
        Application.References.AddFromFile GetSystemDir & "\DevLibOcx.dll"
       End If
#End If
    End If

End Function 
分享