VBA编辑器活动

时间:2016-11-07 16:57:35

标签: vba ms-access access-vba

我在MS Access表单中有一些控件将系统语言更改为土耳其语,阿拉伯语和英语,我想在转到VBA编写代码时将系统语言更改为英语。

我有一个改变系统语言并想知道的代码 如何在激活VBA编辑器时自动运行此代码?

2 个答案:

答案 0 :(得分:2)

如果您在应用程序启动时输入以下代码,则只要按Alt + F11,它就会自动运行Test2。

Private Sub Workbook_Open()

    Application.OnKey "%{F11}", "Test2"

End Sub


Public Sub Test2()

    Debug.Print "tested"

End Sub

我不确定这是否正是你想要的,但这是一个努力实现它。

编辑: 实际上,在这里您可以找到许多有用的东西: http://www.mrexcel.com/forum/excel-questions/468063-determine-language-user.html

E.g。使用Sub ShowLanguages,您可以构建一个函数,告诉您使用的是哪种语言,如果它不是英语,您可以切换到它,就像您在答案中所做的那样。我可能会在以后制作类似的东西。

Private Const LOCALE_ILANGUAGE As Long = &H1
Private Const LOCALE_SCOUNTRY As Long = &H6

Private Declare Function GetKeyboardLayout Lib "user32" _
    (ByVal dwLayout As Long) As Long

Private Declare Function GetLocaleInfo Lib "kernel32" _
    Alias "GetLocaleInfoA" _
    (ByVal Locale As Long, _
    ByVal LCType As Long, _
    ByVal lpLCData As String, _
    ByVal cchData As Long) As Long

Public Sub ShowLangauges()
    Dim hKeyboardID As Long
    Dim LCID As Long

    hKeyboardID = GetKeyboardLayout(0&)
    If hKeyboardID > 0 Then
        LCID = LoWord(hKeyboardID)
        Debug.Print GetUserLocaleInfo(LCID, LOCALE_ILANGUAGE)
        Debug.Print GetUserLocaleInfo(LCID, LOCALE_SCOUNTRY)
    End If
End Sub

Private Function LoWord(wParam As Long) As Integer
    If wParam And &H8000& Then
        LoWord = &H8000& Or (wParam And &H7FFF&)
    Else
        LoWord = wParam And &HFFFF&
    End If
End Function

Public Function GetUserLocaleInfo(ByVal dwLocaleID As Long, _
                                  ByVal dwLCType As Long) As String
    Dim sReturn As String
    Dim nSize As Long
    nSize = GetLocaleInfo(dwLocaleID, dwLCType, sReturn, Len(sReturn))
    If nSize > 0 Then
        sReturn = Space$(nSize)
        nSize = GetLocaleInfo(dwLocaleID, dwLCType, sReturn, Len(sReturn))
        If nSize > 0 Then
            GetUserLocaleInfo = Left$(sReturn, nSize - 1)
        End If
    End If
End Function

答案 1 :(得分:2)

我使用Timer来检查VBA编辑器窗口是否每隔0.5秒是活动窗口,如果是,我运行我的函数将语言更改为英语并停止计时器:

Private Sub Form_Timer()
 Dim st As String
 On Error Resume Next
 st = VBE.ActiveWindow.Caption
 If Err = 0 Then
  ChLng 1033
  Me.TimerInterval = 0
 End If
 On Error GoTo 0
End Sub

当我的表单上的任何控件将语言更改为非英语时,我再次运行Timer:

Private Sub cmbAR_GotFocus()
 ChLng 1025
 Me.TimerInterval = 500
End Sub

Private Sub cmbTR_GotFocus()
 ChLng 1055
 Me.TimerInterval = 500
End Sub

在表单设计中,我手动添加所有需要的事件,包括运行Timer的Form Load事件:

Private Sub Form_Load()
 Me.TimerInterval = 500
End Sub

注意:ChLng xxxx是更改语言的函数:

Private Declare Function ActivateKeyboardLayout Lib _
"user32.dll" (ByVal myLanguage As Long, Flag As Boolean) As Long

'define your desired keyboardlanguage
'find your desired language at http://www.trigeminal.com/frmrpt2dap.asp

Sub ChLng(lng As Long)
 ActivateKeyboardLayout lng, 0
End Sub