自动引用ADO、DAO、Excel-KevinFan
Access软件网QQ交流学习群(群号码198465573),欢迎您的加入!
首页 >技术文章> Access数据库-模块/函数/VBA


自动引用ADO、DAO、Excel

发表时间:2017/12/18 15:39:13 评论(9) 浏览(10179)  评论 | 加入收藏 | 复制
   
摘 要:由于不同电脑的系统环境(操作系统、Office版本)不一致,需要根据系统环境,在程序打开时自动重新引用ADO、DAO、Excel等。
正 文:

      自学了Access一年多,参考了论坛很多大神的作品,自己也陆陆续续开发了一些软件,如进销存、生产统计、财务管理、文件管理,体验了Access快速开发的效率,也为一些兼容性问题头疼,例如开发的系统在不同的电脑上运行,由于不同电脑的系统环境(操作系统、Office版本)不一致,导致用户难于直接运行(有时候未必能满足全部电脑使用相同版本的操作系统和office)。所以根据系统环境,在程序打开时自动重新引用ADO和DAO等就很有必要,论坛也有这样的示例,但较少以系统位数和Office版本来判断,特别是Excel的引用。

      我根据论坛的示例,加上自己的实践,重新整合了ADO、DAO、Excel自动引用,附件我使用2.2的平台添加了自动引用的功能,可以下载参考。

附   件:

点击下载此附件


1、新建一个模块,将文章末尾的代码复制粘贴,然后保存:

点击图片查看大图


2、设计视图打开宏AutoExec,按下图添加执行代码:

点击图片查看大图


'自动重新引用代码:

Option Compare Database
Option Explicit

Public Function ReAddADO()
    On Error Resume Next
    Dim strMDE As String: strMDE = CurrentDb.Properties("MDE")
    If strMDE = "T" Then Exit Function
    Dim REFE As References    '声明REFE为引用
    Dim strFilePath As String    '声明strFilePath为文本型变量
    Dim rf
    Set REFE = Application.References
    For Each rf In REFE    '在引用中循环查找
        If rf.Name = "ado" Then    '如果名字为ADO就移动ADO的引用
            Application.References.Remove rf    '移除
            Exit For    '退出循环
        End If
    Next

    If FolderExists("C:\Windows\SysWOW64") Then   '如果是64位操作系统
        strFilePath = "C:\Program Files (x86)\Common Files\System\ado\msado15.dll"
    Else   '如果是32位操作系统
        strFilePath = "C:\Program Files\Common Files\System\ado\msado15.dll"
    End If
    Set rf = Application.References.AddFromFile(strFilePath)    '重新引用ADO
End Function

Public Function ReAddDAO()
    On Error Resume Next
    Dim strMDE As String: strMDE = CurrentDb.Properties("MDE")
    If strMDE = "T" Then Exit Function
    Dim REFE As References    '声明REFE为引用
    Dim strFilePath As String    '声明strFilePath为文本型变量
    Dim rf
    Set REFE = Application.References
    For Each rf In REFE    '在引用中循环查找
        If rf.Name = "DAO" Then    '如果名字为DAO就移动DAO的引用
            Application.References.Remove rf    '移除
            Exit For    '退出循环
        End If
    Next

    If FolderExists("C:\Windows\SysWOW64") Then    '如果是64位操作系统
        strFilePath = "C:\Program Files (x86)\Common Files\microsoft shared\DAO\dao360.dll"
    Else   '如果是32位操作系统
        strFilePath = "C:\Program Files\Common Files\Microsoft Shared\DAO\dao360.dll"
    End If
    Set rf = Application.References.AddFromFile(strFilePath)    '重新引用DAO
End Function


Public Function ReAddExcel()
    On Error Resume Next
    Dim strMDE As String: strMDE = CurrentDb.Properties("MDE")
    If strMDE = "T" Then Exit Function
    Dim REFE As References    '声明REFE为引用
    Dim strFilePath As String    '声明strFilePath为文本型变量
    Dim rf
    Set REFE = Application.References
    For Each rf In REFE    '在引用中循环查找
        If rf.Name = "Excel" Then    '如果名字为Excel就移动Excel的引用
            Application.References.Remove rf    '移除
            Exit For    '退出循环
        End If
    Next

    '这里的Excel启动路径为您电脑上Excel所在的Office文件夹盘符路径,2003版本为Office11、
    '2007版本为Office12、2010版本为Office14、2013版本为Office15、2016版本为Office16

    '如果是Office2003:
    If FolderExists("C:\Program Files (x86)\Microsoft Office\Office11") Then
        strFilePath = "C:\Program Files (x86)\Microsoft Office\Office11\EXCEL.EXE"
    ElseIf FolderExists("C:\Program Files\Microsoft Office\Office11") Then
        strFilePath = "C:\Program Files\Microsoft Office\Office11\EXCEL.EXE"

        '如果是Office2007:
    ElseIf FolderExists("C:\Program Files (x86)\Microsoft Office\Office12") Then
        strFilePath = "C:\Program Files (x86)\Microsoft Office\Office12\EXCEL.EXE"
    ElseIf FolderExists("C:\Program Files\Microsoft Office\Office12") Then
        strFilePath = "C:\Program Files\Microsoft Office\Office12\EXCEL.EXE"

        '如果是Office2010:
    ElseIf FolderExists("C:\Program Files (x86)\Microsoft Office\Office14") Then
        strFilePath = "C:\Program Files (x86)\Microsoft Office\Office14\EXCEL.EXE"
    ElseIf FolderExists("C:\Program Files\Microsoft Office\Office14") Then
        strFilePath = "C:\Program Files\Microsoft Office\Office14\EXCEL.EXE"

        '如果是Office2013:
    ElseIf FolderExists("C:\Program Files (x86)\Microsoft Office\Office15") Then
        strFilePath = "C:\Program Files (x86)\Microsoft Office\Office15\EXCEL.EXE"
    ElseIf FolderExists("C:\Program Files\Microsoft Office\Office15") Then
        strFilePath = "C:\Program Files\Microsoft Office\Office15\EXCEL.EXE"

        '如果是Office2016或Office365:
    ElseIf FolderExists("C:\Program Files (x86)\Microsoft Office\Office16") Then
        strFilePath = "C:\Program Files (x86)\Microsoft Office\Office16\EXCEL.EXE"
    ElseIf FolderExists("C:\Program Files\Microsoft Office\Office16") Then
        strFilePath = "C:\Program Files\Microsoft Office\Office16\EXCEL.EXE"
    End If

    Set rf = Application.References.AddFromFile(strFilePath)    '自动引用Excel
End Function

Function FileExists(ByVal strFile As String, Optional bFindFolders As Boolean) As Boolean
    Dim lngAttributes As Long
    lngAttributes = (vbReadOnly or vbHidden or vbSystem)
    If bFindFolders Then
        lngAttributes = (lngAttributes or vbDirectory)
    Else
        Do While Right$(strFile, 1) = "\"
            strFile = Left$(strFile, Len(strFile) - 1)
        Loop
    End If

    On Error Resume Next
    FileExists = (Len(Dir(strFile, lngAttributes)) > 0)
End Function

Function FolderExists(strPath As String) As Boolean
    On Error Resume Next
    FolderExists = ((GetAttr(strPath) And vbDirectory) = vbDirectory)
End Function


Access软件网交流QQ群(群号:198465573)
 
 相关文章
【Access懒人工具】用VBA代码重新引用DAO  【Bobby  2013/11/25】
【Access懒人工具】用VBA代码重新引用ADO  【麥田  2013/12/5】
【Access懒人工具】用VBA代码自动引用树控件|代码引用Mic...  【麥田  2013/12/6】
【Access懒人工具】用VBA代码自动引用OLE项|代码引用OL...  【麥田  2013/12/7】
【Access懒人工具】用VBA代码自动引用Excel项  【麥田  2016/9/19】
常见问答
技术分类
相关资源
文章搜索
关于作者

KevinFan

文章分类

文章存档

友情链接