修复编译错误 – 预期标识符的方法是什么?

huangapple go评论89阅读模式
英文:

How to go about repairing Compile error - expected identifier

问题

请问有人可以帮我解决以下错误信息吗?
前两行都以红色高亮显示:

Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, _
ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

Public Const VK_SNAPSHOT = 44

Public Const VK_LMENU = 164

Public Const KEYEVENTF_KEYUP = 2

Public Const KEYEVENTF_EXTENDEDKEY = 1

我们的组织使用Excel插件从电子表格中提取信息,这仅适用于32位Office。在一台笔记本电脑上,我没有任何问题,它按预期工作,但在新的笔记本电脑上却一直出现“expected identifier”错误。

有人能帮忙吗?

英文:

could someone point me in the right direction with the following error message please.
The first two lines are both highlighted in red:

Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, _

ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

Public Const VK_SNAPSHOT = 44

Public Const VK_LMENU = 164

Public Const KEYEVENTF_KEYUP = 2

Public Const KEYEVENTF_EXTENDEDKEY = 1

Our organisation uses an Excel add in to pull the information from the spreadsheet which will only work with 32bit office. I'm working off 2 laptops, on one I have no issues it works as it should but on the new laptop keeps coming up with expected identifier error.

Could someone help?

答案1

得分: 1

你遇到的错误可能是因为32位库与64位Office版本不兼容。如果你在64位Office上运行这段代码,你需要将API调用声明为"PtrSafe"。

以下是你应该为64位Office声明keybd_event的方式:

#If VBA7 Then
    Declare PtrSafe Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As LongPtr)
#Else
    Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
#End If

VBA7条件常量在Office 2010及以后的版本中为True,因此这样做可以确保你的代码在32位和64位Office版本中都能正常工作。此外,在64位VBA中,引入了一个LongPtr数据类型,它可以容纳32位或64位的内存地址,具体取决于版本。在你的情况下,dwExtraInfo应该是一个LongPtr。

你可以以这种方式修改所有的API调用,以确保在32位和64位Office版本中都能兼容。

请注意,这个修复方案假设你遇到的问题是因为32位和64位之间的不一致而引起的。如果错误仍然存在,可能是你的代码中的其他问题。

英文:

The error you're encountering could be due to the incompatibility of 32-bit libraries with 64-bit versions of Office. If you're running this code on 64-bit Office, you'll need to declare your API calls as "PtrSafe".

Here's how you should declare your keybd_event for 64-bit Office:

#If VBA7 Then
    Declare PtrSafe Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As LongPtr)
#Else
    Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
#End If

The VBA7 conditional constant is True for Office 2010 and later, so this way your code will work for both 32-bit and 64-bit versions of Office. Also, in 64-bit VBA, a LongPtr data type was introduced that can hold either a 32-bit or 64-bit memory address, depending on the version. In your case, dwExtraInfo should be a LongPtr.

You can modify all your API calls this way to ensure compatibility across 32-bit and 64-bit Office versions.

Please note that this fix assumes that you're encountering an issue because of a 32-bit vs 64-bit discrepancy. If the error persists, it might be due to another issue within your code.

答案2

得分: 0

这是由于在行连续字符“_”之后有一个空行引起的编译时错误。

可以按照以下描述精确复制此错误:

修复编译错误 – 预期标识符的方法是什么?

删除空行将解决问题。

英文:

This is a compile-time error caused by the a blank line after the line-continuation character _

Can replicate this exactly as described:

修复编译错误 – 预期标识符的方法是什么?

Removing the blank line will fix the problem.

huangapple
  • 本文由 发表于 2023年6月6日 05:36:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76410157.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定