新建From1(窗体),新建Command1(按钮CommandButton),代码:
#If VBA7 Then '有这个即使不是VB6的语法,也不会提示出错,放外面就VB6就提示出错。
Private Declare PtrSafe Function CommDlgExtendedError Lib "comdlg32.dll" () As Long
#End If
#If 1 = 2 Then '可以看到 VBA7 只是个最新变量,用其他条件也可以不出错
Private Declare PtrSafe Function ts_apiGetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (tsFN As tsFileName) As Boolean
#End If
Private Sub Command1_Click()
Call s_1
End Sub
Public Sub s_1()
Call s_Num
#If VBA7 Then
Debug.Print "7 If" & VBA7 '语句
#Else
Debug.Print "6 If" & VBA7
#End If
End Sub
#If VBA7 Then
Public Sub s_Num() '过程
Debug.Print "7 VBA=" & VBA7
End Sub
#Else
Public Sub s_Num()
Debug.Print "6 VBA=" & VBA7
End Sub
#End If
在VB6中运行结果是:
6 VBA=
6 If
在Office VBA或WPS VBA的模块中(记得使用Public而不是Private)运行结果是:
7 VBA=
7 If
可以看到,VBA7都是空。