使用VBA将区域格式更改为另一种语言 [英] Change region format to another language with VBA
问题描述
我需要将区域格式更改为法语(加拿大)" ,以使datevalue
接受法国月份(例如,火星05,艾薇儿12)等,然后在VBA代码中将其恢复为英语(加拿大)" .
I need to change the region format to "French (Canada)" in order for datevalue
to accept french months (eg. 05 Mars, 12 Avril, etc) and then revert it back to "English (Canada)" within the VBA code.
我希望它不会那么复杂,并且有一个写属性可以使用VBA修改此设置.
I hope it's not as complex and that there is a write property that can modify this setting with VBA.
到目前为止,我已经找到了Application.International(xlCountrySetting),但这只是一个读取属性.
So far, I've found Application.International(xlCountrySetting) but it is only a read property.
这是我要更改的有问题的设置:
This is the setting in question I wish to change:
谢谢
推荐答案
正如注释中指出的,您可以通过几个简单的Windows API调用来完成此操作.建议不要使用GetUserDefaultLCID
测试当前设置,而不是假设当前将计算机设置为英语(加拿大)".
As pointed out in the comments, you can do this with a couple simple Windows API calls. Instead of assuming that the machine is currently set to "English (Canada)", I'd suggest testing the current setting with GetUserDefaultLCID
, and then setting it back to that when you're done.
#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
相关功能的文档链接如下:
Documentation links for the relevant functions are below:
- SetThreadLocale
- GetUserDefaultLCID
- LocaleNameToLCID
这篇关于使用VBA将区域格式更改为另一种语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!