您对 ActivateKeyboardLayout 的声明实际上是不正确的。对于 32 位系统,您的代码应该是这样的:
Private Declare Function ActivateKeyboardLayout Lib "user32" (ByVal HKL As Long, _
ByVal flags As Long) As Long
Const aklPUNJABI As Long = &H4460446
Dim oldLayout as Long
oldLayout = ActivateKeyboardLayout(aklPUNJABI, 0)
If oldLayout = 0 Then
'Oops an error'
Else
'Save old layout for later restore?'
End If
在这种情况下,操作系统的 64 位有点像红鲱鱼。由于您正在运行 VBA 应用程序,因此无论操作系统如何,它都必须作为 32 位应用程序运行。我怀疑您的问题可能是在您的 Vista 系统上未加载您想要的旁遮普语键盘布局。ActivateKeyboardLayout 仅用于激活已加载的键盘布局。出于某种原因,此 API 的设计者认为由于键盘布局不存在而导致的失败不是错误,因此未设置 LastDllError。对于这种情况,您可能需要考虑使用 LoadKeyboardLayout。
编辑:要仔细检查您尝试获取的键盘布局是否已实际加载,您可以使用以下命令:
Private Declare Function GetKeyboardLayoutList Lib "user32" (ByVal size As Long, _
ByRef layouts As Long) As Long
Dim numLayouts As Long
Dim i As Long
Dim layouts() As Long
numLayouts = GetKeyboardLayoutList(0, ByVal 0&)
ReDim layouts(numLayouts - 1)
GetKeyboardLayoutList numLayouts, layouts(0)
Dim msg As String
msg = "Loaded keyboard layouts: " & vbCrLf & vbCrLf
For i = 0 To numLayouts - 1
msg = msg & Hex(layouts(i)) & vbCrLf
Next
MsgBox msg