如何通过Excel中的宏更改Windows的语言?

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

How to change the language of Windows by a macro in excel?

问题

我创建了一个表单。它的名字是FF,该表单通过一个简单的模块显示:

Sub SH()
    FF.Show = True 
End Sub

这个表单包含一些按钮,我使用以下代码为这些按钮设置了快捷键:

CommandButton1.Accelerator = "H"
CommandButton2.Accelerator = "A"
Command...

如果Windows的语言是英文并且我显示表单,快捷键可以正常工作,但如果不是英文并且显示表单,那么快捷键将无法工作(即使我按下"Alt+左Shift")。

因此,我必须在显示表单之前按下"Alt+左Shift"。但这可能会被遗忘... 我想我应该在"FF.Show = True"之前放一些代码来将Windows的语言更改为英文。是否有任何代码可以解决我的问题?

感谢您的帮助。

英文:

I have created a form. It's name is FF and the form
is showed with a simple module:

sub SH()
FF.show=true 
End sub

This form contains some buttons and I have put shortcuts for these buttons with
the following codes:

CommandButton1.Accelerator = "H"
CommandButton2.Accelerator = "A"
Command...

If the language of Windows is English and I show the form,
the shortcuts work correctly, but if it is not English
and the form is shown, then the shortcuts does not work (even if I press the "Alt+leftShift").

So I have to press "Alt+leftShift" before I show the form.
But this can be forgettable and... .I think I shoud put some codes before "FF.show=true"
to change the language of windows to English. Is there any code that solve my problem?

Thank you for your help

答案1

得分: 1

Two languages are installed on my computer. I am using two Functions to change the keyboard through VB

Option Explicit

#If VBA7 Then
Private Declare PtrSafe Function ActivateKeyboardLayout Lib "user32" (ByVal HKL As LongPtr, ByVal flag As Long) As LongPtr
#Else
Private Declare Function ActivateKeyboardLayout Lib "user32.dll" (ByVal HKL As Long, flag As Long) As Long
#End If

Public Function ELLHNIKA() As Integer
   If ActivateKeyboardLayout(1032, 0) = 0 Then
      MsgBox ("Unable to select Greek keyboard")
      ELLHNIKA = -1
   Else
      ELLHNIKA = 0
   End If
End Function

Public Function ENGLISH() As Integer
   If ActivateKeyboardLayout(1033, 0) = 0 Then
      MsgBox ("Unable to select English keyboard")
      ENGLISH = -1
   Else
      ENGLISH = 0
   End If
End Function

If you want to switch between the installed languages then use this version:

Public Sub NextLang()
   Call ActivateKeyboardLayout(1, 0)
End Sub

Reference

英文:

Two languages are installed on my computer. I am using two Functions to change the keyboard through VB

Option Explicit

#If VBA7 Then
Private Declare PtrSafe Function ActivateKeyboardLayout Lib "user32" (ByVal HKL As LongPtr, ByVal flag As Long) As LongPtr
#Else
Private Declare Function ActivateKeyboardLayout Lib "user32.dll" (ByVal HKL As Long, flag As Long) As Long
#End If


Public Function ELLHNIKA() As Integer
   If ActivateKeyboardLayout(1032, 0) = 0 Then
      MsgBox ("Unable to select Greek keyboard")
      ELLHNIKA = -1
   Else
      ELLHNIKA = 0
   End If
End Function

Public Function ENGLISH() As Integer
   If ActivateKeyboardLayout(1033, 0) = 0 Then
      MsgBox ("Unable to select English keyboard")
      ENGLISH = -1
   Else
      ENGLISH = 0
   End If
End Function

If you want to switch between the installed languages then use this version:

Public Sub NextLang()
   Call ActivateKeyboardLayout(1, 0)
End Sub

https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-activatekeyboardlayout

huangapple
  • 本文由 发表于 2023年6月8日 21:58:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76432634.html
匿名

发表评论

匿名网友

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

确定