首页 > 解决方案 > 将区域设置从波兰语更改为美国 windows 10

问题描述

我正在尝试使用此代码:

#If VBA7 Then
    Private Declare PtrSafe Function SetThreadLocale Lib "kernel32" _
        (ByVal Locale As Long) As Boolean
    Private Declare PtrSafe Function GetUserDefaultLCID Lib "kernel32" () As Long
    Private Declare PtrSafe Function LocaleNameToLCID Lib "kernel32" _
        (ByVal lpName As LongPtr, dwFlags As Long) As Long
#Else
    Private Declare Function SetThreadLocale Lib "kernel32" (ByVal Locale As Long) As Boolean
    Private Declare Function GetUserDefaultLCID Lib "kernel32" () As Long
    Private Declare Function LocaleNameToLCID Lib "kernel32" _
       (ByVal lpName As LongPtr, dwFlags As Long) As Long
#End If

Private Sub Test()
    'Get the locale identifier for French (Canada)
    Dim frCa As Long
    frCa = LocaleNameToLCID(StrPtr("fr-CA"), 0)
    'Make sure there function succeeded.
    If result = 0 Then
        'Cache the current locale
        Dim userLocale As Long
        userLocale = GetUserDefaultLCID
        'Switch to French (Canada)
        If SetThreadLocale(frCa) Then
            'en français
            '...
            'switch back
            SetThreadLocale userLocale
        End If
    End If
End Sub

来自链接:

使用 VBA 将区域格式更改为另一种语言

我正在寻找类似的解决方案,但上述方法不起作用。我尝试将区域设置更改为美国设置并恢复为波兰区域设置(Windows 默认设置)。

GetUserDefaultLCID = 1045, US = 1033 的波兰设置,但宏运行时没有任何结果。(没有错误,没有变化)。某种 SetThreadLocale 不起作用...

请帮助可能出现的问题,Best,Jacek

当我使用带有我们区域设置的 Windows 但没有工作时,我尝试使用而不是“fr-CA”“pl-PL”并分配给 userLocale = 1045。

目标是让这个宏在 Windows 10 中工作。

标签: excelvba

解决方案


请注意,稍后If SetThreadLocale(frCa) Then切换到frCa1 行(在评论之后),您切换之前的内容SetThreadLocale userLocale(如果您阅读了链接的原始线程,它会告诉您这一点)。

所以这种变化只在这两个语句之间发生了很短的时间。

因此,要么将应该在更改后的语言环境上运行的代码放在两者之间:

If SetThreadLocale(frCa) Then
    'put your code that should run in polish here

    SetThreadLocale userLocale
End If

或者要永久更改语言环境,请使用以下内容而不是上述内容:

SetThreadLocale frCa

推荐阅读