VB6 的编译预处理中 VBA7 VBA6 Win64 Win32 怎么看,怎么在立即窗口打印不出来?如何获得VBA是32位还是64位? - 第1页 - 码农库
 
首页 /编程语言和算法/VB6/VBA/ASP/TB
 VB6 的编译预处理中 VBA7 VBA6 Win64 Win32 怎么看,怎么在立即窗口打印不出来?如何获得VBA是32位还是64位?
2026年7月10日 18:08

在Office2016(32位)上放一个 test.xlsm,代码:

#If VBA7 Then
    Private Declare PtrSafe Function GetSystemWow64Directory Lib "Kernel32.dll" Alias "GetSystemWow64DirectoryA" (ByVal lpBuffer As String, ByVal uSize As Long) As Long
#Else
    Private Declare Function GetSystemWow64Directory Lib "Kernel32.dll" Alias "GetSystemWow64DirectoryA" (ByVal lpBuffer As String, ByVal uSize As Long) As Long
#End If

Public Sub s_GetOp()
    Dim DirPath As String, Result As Long
     
    DirPath = Space(255)
    Result = GetSystemWow64Directory(DirPath, 255)
     
    MsgBox IIf(Result <> 0, 64, 32) & "位操作系统"
End Sub

Private Sub s_GetVBA_Bit()
    Dim strText As String
    strText = Environ("PROCESSOR_ARCHITECTURE")
    Debug.Print strText
    
    If strText Like "*64*" Then
        MsgBox "当前使用的是64位VBA"
    ElseIf strText Like "*86*" Then
        MsgBox "当前使用的是32位VBA"
    End If
End Sub

Private Sub CommandButton1_Click()
    Call s_GetOp
    
    #If VBA7 = 1 Then
        MsgBox "VBA7"
    #End If
    
    Call s_GetVBA_Bit
End Sub

运行结果是提示我是 64位操作系统。

就算你输入 vba7 也会变成大写  VBA7,输入 vba6 也会变成大写  VBA6,win64会变成Win64,win32会变成Win32,但是其它就不会变,还会出错。也就是说这4个是关键词。

如果我改成:

#If VBA8 Then

哈哈,那么就对Private Declare Function 这句报错!因为没有VBA8这个关键词。

经过测试 VBA7 VBA6 Win64 Win32 是等价的,都是1,而不是True(#If VBA7 = True Then会不成立)。

而在立即窗口中,Print VBA7 显示空,只能在代码区使用。


如何获得VBA是32位还是64位?

安装Office2016(32位)和WPS(64位),在VBA中,可以得到32位和64位的提示。


注意:VB7和Win64是不一样的!

' 只判断32/64位 → 用1个即可
#If Win64 Then
    ' 64位代码
#Else
    ' 32位代码
#End If

' 判断新旧VBA版本 → 用VBA7
#If VBA7 Then
    ' 现代VBA (支持LongPtr等)
#Else
    ' 旧版VBA
#End If

' 需要精确区分3种情况 → 用2个嵌套
#If VBA7 Then
    #If Win64 Then
        ' 64位新VBA
    #Else
        ' 32位新VBA
    #End If
#Else
    ' 旧版VBA (必为32位)
#End If

场景-VBA7-Win64

Office2013(32位)-True-False    

Office2013 (64位)-True-True    


 
全部回复(0)
首页 | 电脑版 |