在XP下运行VBA我能够调用ActivateKeyboardLayout将输入语言从英语切换到另一种语言.但是,这在Vista64下不再有效.
有任何建议或解决方法吗?
以前在XP下工作的代码类似于以下内容:
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
当我尝试这个时,我收到错误消息:
变量使用Visual Basic中不支持的自动化类型
您对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