在 Excel 中声明 PtrSafe 兼容性

Joa*_*lai 4 excel vba

我在 VBA 中使用这个语句

Private Declare PtrSafe Function GetTimeZoneInformationAny Lib "kernel32" Alias _
  "GetTimeZoneInformation" (buffer As Any) As Long
Run Code Online (Sandbox Code Playgroud)

这适用于:

  • Win 64 位,Excel 2010 64 位
  • Win 64 位,Excel 1010 32 位

但是我的客户报告了一个关于 word 的编译错误PtrSafe。他正在使用 Excel 2010。

我不确定为什么会发生这种情况。有任何想法吗?

Joa*_*lai 6

好的,我已经重写了这个:

#If VBA7 Then
Private Declare PtrSafe Function GetTimeZoneInformationAny Lib "kernel32" Alias "GetTimeZoneInformation" (buffer As Any) As Long
#Else
Private Declare Function GetTimeZoneInformationAny Lib "kernel32" Alias "GetTimeZoneInformation" (buffer As Any) As Long
#End If
Run Code Online (Sandbox Code Playgroud)

这在 Excel 2010 64 位的第二行出现编译错误,该行保持红色,但是当我再次编译时,错误不再出现。这是 Excel 的行为方式吗?

  • 是的,在 IDE 中,未使用的行将保持红色,如果您尝试手动更改它,则会导致出现错误消息框(除非它是 Office 2010+ 32 位,其中两行都有效,因此都不会变红) . 消息框通常非常烦人,不仅在声明 API 时,所以我个人更喜欢取消勾选“工具 - 选项 - 编辑器 - 自动语法检查”。尽管名称如此,但这会保持语法检查,仅抑制消息框。 (6认同)