Search code examples
excelvbashortcut

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


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


Solution

  • 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