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

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

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

问题

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

  1. Sub SH()
  2. FF.Show = True
  3. End Sub

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

  1. CommandButton1.Accelerator = "H"
  2. CommandButton2.Accelerator = "A"
  3. 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:

  1. sub SH()
  2. FF.show=true
  3. End sub

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

  1. CommandButton1.Accelerator = "H"
  2. CommandButton2.Accelerator = "A"
  3. 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

  1. Option Explicit
  2. #If VBA7 Then
  3. Private Declare PtrSafe Function ActivateKeyboardLayout Lib "user32" (ByVal HKL As LongPtr, ByVal flag As Long) As LongPtr
  4. #Else
  5. Private Declare Function ActivateKeyboardLayout Lib "user32.dll" (ByVal HKL As Long, flag As Long) As Long
  6. #End If
  7. Public Function ELLHNIKA() As Integer
  8. If ActivateKeyboardLayout(1032, 0) = 0 Then
  9. MsgBox ("Unable to select Greek keyboard")
  10. ELLHNIKA = -1
  11. Else
  12. ELLHNIKA = 0
  13. End If
  14. End Function
  15. Public Function ENGLISH() As Integer
  16. If ActivateKeyboardLayout(1033, 0) = 0 Then
  17. MsgBox ("Unable to select English keyboard")
  18. ENGLISH = -1
  19. Else
  20. ENGLISH = 0
  21. End If
  22. End Function

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

  1. Public Sub NextLang()
  2. Call ActivateKeyboardLayout(1, 0)
  3. End Sub

Reference

英文:

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

  1. Option Explicit
  2. #If VBA7 Then
  3. Private Declare PtrSafe Function ActivateKeyboardLayout Lib "user32" (ByVal HKL As LongPtr, ByVal flag As Long) As LongPtr
  4. #Else
  5. Private Declare Function ActivateKeyboardLayout Lib "user32.dll" (ByVal HKL As Long, flag As Long) As Long
  6. #End If
  7. Public Function ELLHNIKA() As Integer
  8. If ActivateKeyboardLayout(1032, 0) = 0 Then
  9. MsgBox ("Unable to select Greek keyboard")
  10. ELLHNIKA = -1
  11. Else
  12. ELLHNIKA = 0
  13. End If
  14. End Function
  15. Public Function ENGLISH() As Integer
  16. If ActivateKeyboardLayout(1033, 0) = 0 Then
  17. MsgBox ("Unable to select English keyboard")
  18. ENGLISH = -1
  19. Else
  20. ENGLISH = 0
  21. End If
  22. End Function

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

  1. Public Sub NextLang()
  2. Call ActivateKeyboardLayout(1, 0)
  3. 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:

确定