VBA 32位代码适配64位

     很久前用 Access VBA 写个程序,在64位的 Office 下一直报错,遂一直用 32位的 Office。最近尝试安装了 Office 2021,看到那图标那界面顿时爱了。一时没有找到 32位 的 Office 2021,遂决定解决代码的适配问题,好一劳永逸。

1)在操作具体模块时,弹出“Microsoft Visual Basic for Applications”窗口,报:

     隐藏的模块中的编译错误:mod_openFile。
     此错误通常会在代码与此应用程序的版本、平台或基础结构不兼容时发生。单击“帮助”以获取如何更正此错逞的信息。


2)进入名称为“mod_openFile”模块(个人自定义的),相关代码红色显示(32位 Office正常),报:

      编译错误:
      若要在64位系统上使用,则必须更新此项目中的代码。请检查并更新 Declare 语句,然后用 PtrSafe 属性标记它们。

3)解决方法确实很简单,只需在 Declare 后加上 PtrSafe即可

'原代码段
    Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    Public Declare Function SetCursor Lib "user32" (ByVal hCursor As Long) As Long
    Public Declare Function LoadCursorByNum Lib "user32" Alias "LoadCursorA" (ByVal hInstance As Long, ByVal lpCursorName As Long) As Long
'更新后代码段
    Public Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    Public Declare PtrSafe Function SetCursor Lib "user32" (ByVal hCursor As Long) As Long
    Public Declare PtrSafe Function LoadCursorByNum Lib "user32" Alias "LoadCursorA" (ByVal hInstance As Long, ByVal lpCursorName As Long) As Long

你可能感兴趣的:(开发测试,适配64位)