如何使用VBA从64位Windows Vista调用ActivateKeyboardLayout [英] How can I call ActivateKeyboardLayout from 64bit Windows Vista using VBA

查看:238
本文介绍了如何使用VBA从64位Windows Vista调用ActivateKeyboardLayout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在XP下运行VBA,我能够调用ActivateKeyboardLayout将输入语言从英语切换到另一种语言.但是,这在Vista64下不再起作用.

Running VBA under XP I was able to call ActivateKeyboardLayout to switch my input language from English to another language. However, this no longer works under Vista64.

有什么建议或解决方法吗?

Any suggestions or workarounds?

过去在XP下可以运行的代码类似于以下内容:

The code that used to work under XP was similar to the following:

Private Declare Function ActivateKeyboardLayout Lib "user32" ( _
    ByVal HKL As Long, ByVal flags As Integer) As Integer
Const aklPUNJABI As Long = &H4460446
ActivateKeyboardLayout aklPUNJABI, 0

有人建议尝试

Public Declare Function ActivateKeyboardLayout Lib "user32" ( _
    ByVal nkl As IntPtr, ByVal Flags As uint) As Integer

当我尝试此操作时,我收到错误消息:

When I try this I get the error message:

变量使用Visual Basic不支持的自动化类型

推荐答案

您对ActivateKeyboardLayout的声明实际上是错误的.对于32位系统,您的代码应如下所示:

Your declaration for the ActivateKeyboardLayout is actually incorrect. For 32-bit systems your code should be something like this:

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用于这种情况.

The 64-bitness of the operating system is a bit of a red herring in this case. Since you are running a VBA app it must be running as a 32-bit app regardless of OS. I suspect your problem may be that on your Vista system the Punjabi keyboard layout that you want is not loaded. ActivateKeyboardLayout will only work to activate a keyboard layout that is already loaded. For some reason the designers of this API felt that failure due to the keyboard layout not existing was not an error so the LastDllError is not set. You may want to look into using LoadKeyboardLayout for this type of situation.

要仔细检查您尝试获取的键盘布局是否已实际加载,可以使用以下方法:

To double check that the keyboard layout you are trying to get is actually loaded you can use this:

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

这篇关于如何使用VBA从64位Windows Vista调用ActivateKeyboardLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆