如何用vba枚举系统中所有已安装或者支持的LCID?

EnumSystemLocalesEx函数可以枚举所有已安装或者系统支持的区域。

它的语法如下:

BOOL EnumSystemLocalesEx(
  LOCALE_ENUMPROCEX lpLocaleEnumProcEx,
  DWORD             dwFlags,
  LPARAM            lParam,
  LPVOID            lpReserved
);

其中lpLocaleEnumProcEx参数为指向回调函数EnumLocalesProcEx的指针。

回调函数EnumLocalesProcEx的语法如下:

BOOL CALLBACK EnumLocalesProcEx(
  _In_ LPWSTR lpLocaleString,
  _In_ DWORD  dwFlags,
  _In_ LPARAM lParam
);

其中参数lpLocaleString 为指向区域名称的缓冲区的指针。

将它们转换为vba 声明如下:

Declare Function EnumSystemLocalesEx Lib "kernel32" (ByVal lpLocaleEnumProcEx As Long, ByVal dwFlags As Long, ByVal lParam As Long, ByVal lpReserved As Long) As Long
Function EnumLocalesProcEx(ByVal lpLocaleString As Long, ByVal dwFlags As Long, ByVal lParam As Long) As Long
    EnumLocalesProcEx = 1
End Function

两个需要特别注意的是:回调函数的返回值必须为1,才能遍历枚举,另外在vba声明中lpLocaleString参数必须为long类型,存储的是地址,否则也无法返回正确值。

综合以上知识,可以使用如下的代码:

Declare Function LCIDToLocaleName Lib "kernel32" (ByVal Locale As Long, ByVal lpName As String, ByVal cchName As Long, ByVal dwFlags As Long) As Long
Declare Function LocaleNameToLCID Lib "kernel32" (ByVal lpName As Long, ByVal dwFlags As Long) As Long
Declare Function GetUserDefaultLCID Lib "kernel32" () As Long
Declare Function GetSystemDefaultLCID Lib "kernel32" () As Long
Declare Function EnumSystemLocalesEx Lib "kernel32" (ByVal lpLocaleEnumProcEx As Long, ByVal dwFlags As Long, ByVal lParam As Long, ByVal lpReserved As Long) As Long
Const LOCALE_NAME_MAX_LENGTH = 85
Const LCID_INSTALLED = 1
Const LCID_SUPPORTED = 2
Const LCID_ALTERNATE_SORTS = 4
Sub QQ1722187970()
    Dim oWK As Worksheet
    Set oWK = Excel.ActiveSheet
    oWK.Cells.Clear
    EnumSystemLocalesEx AddressOf EnumLocalesProcEx, 0, 0, 0
End Sub
Function EnumLocalesProcEx(ByVal lpLocaleString As Long, ByVal dwFlags As Long, ByVal lParam As Long) As Long
    iLCID = LocaleNameToLCID(lpLocaleString, 0)
    Dim sLocaleName As String
    'LCID2LocaleName
    sLocaleName = Space(LOCALE_NAME_MAX_LENGTH)
    iLen = LCIDToLocaleName(iLCID, sLocaleName, LOCALE_NAME_MAX_LENGTH, 0) - 1
    sLocaleName = VBA.Replace(sLocaleName, Chr(0), "")
    sLocaleName = Left(sLocaleName, iLen)
    Dim oWK As Worksheet
    Set oWK = Excel.ActiveSheet
    oWK.Range("a1:c1") = Array("LCID(16进制)", "LCID(10进制)", "LocaleName")
    iRow = oWK.Range("a65536").End(xlUp).Row + 1
    With oWK
        .Cells(iRow, "A") = VBA.Hex(iLCID)
        .Cells(iRow, "B") = iLCID
        .Cells(iRow, "C") = sLocaleName
    End With
    EnumLocalesProcEx = 1
End Function

下表是枚举后的结果:

LCID(16进制) LCID(10进制) LocaleName
401 1025 ar-SA
402 1026 bg-BG
403 1027 ca-ES
804 2052 zh-CN
405 1029 cs-CZ
406 1030 da-DK
407 1031 de-DE
408 1032 el-GR
409 1033 en-US
C0A 3082 es-ES
40B 1035 fi-FI
40C 1036 fr-FR
40D 1037 he-IL
40E 1038 hu-HU
40F 1039 is-IS
410 1040 it-IT
411 1041 ja-JP
412 1042 ko-KR
413 1043 nl-NL
414 1044 nb-NO
415 1045 pl-PL
416 1046 pt-BR
417 1047 rm-CH
418 1048 ro-RO
419 1049 ru-RU
41A 1050 hr-HR
41B 1051 sk-SK
41C 1052 sq-AL
41D 1053 sv-SE
41E 1054 th-TH
41F 1055 tr-TR
420 1056 ur-PK
421 1057 id-ID
422 1058 uk-UA
423 1059 be-BY
424 1060 sl-SI
425 1061 et-EE
426 1062 lv-LV
427 1063 lt-LT
428 1064 tg-Cyrl-TJ
429 1065 fa-IR
42A 1066 vi-VN
42B 1067 hy-AM
42C 1068 az-Latn-AZ
42D 1069 eu-ES
42E 1070 hsb-DE
42F 1071 mk-MK
432 1074 tn-ZA
434 1076 xh-ZA
435 1077 zu-ZA
436 1078 af-ZA
437 1079 ka-GE
438 1080 fo-FO
439 1081 hi-IN
43A 1082 mt-MT
43B 1083 se-NO
83C 2108 ga-IE
43E 1086 ms-MY
43F 1087 kk-KZ
440 1088 ky-KG
441 1089 sw-KE
442 1090 tk-TM
443 1091 uz-Latn-UZ
444 1092 tt-RU
445 1093 bn-IN
446 1094 pa-IN
447 1095 gu-IN
448 1096 or-IN
449 1097 ta-IN
44A 1098 te-IN
44B 1099 kn-IN
44C 1100 ml-IN
44D 1101 as-IN
44E 1102 mr-IN
44F 1103 sa-IN
450 1104 mn-MN
451 1105 bo-CN
452 1106 cy-GB
453 1107 km-KH
454 1108 lo-LA
456 1110 gl-ES
457 1111 kok-IN
45A 1114 syr-SY
45B 1115 si-LK
85D 2141 iu-Latn-CA
45E 1118 am-ET
85F 2143 tzm-Latn-DZ
461 1121 ne-NP
462 1122 fy-NL
463 1123 ps-AF
464 1124 fil-PH
465 1125 dv-MV
468 1128 ha-Latn-NG
46A 1130 yo-NG
46B 1131 quz-BO
46C 1132 nso-ZA
46D 1133 ba-RU
46E 1134 lb-LU
46F 1135 kl-GL
470 1136 ig-NG
478 1144 ii-CN
47A 1146 arn-CL
47C 1148 moh-CA
47E 1150 br-FR
7F 127
480 1152 ug-CN
481 1153 mi-NZ
482 1154 oc-FR
483 1155 co-FR
484 1156 gsw-FR
485 1157 sah-RU
486 1158 qut-GT
487 1159 rw-RW
488 1160 wo-SN
48C 1164 prs-AF
491 1169 gd-GB
404 1028 zh-TW
40A 1034 es-ES_tradnl
45D 1117 iu-Cans-CA
801 2049 ar-IQ
807 2055 de-CH
809 2057 en-GB
80A 2058 es-MX
80C 2060 fr-BE
810 2064 it-CH
813 2067 nl-BE
814 2068 nn-NO
816 2070 pt-PT
81A 2074 sr-Latn-CS
81D 2077 sv-FI
82C 2092 az-Cyrl-AZ
82E 2094 dsb-DE
83B 2107 se-SE
83E 2110 ms-BN
843 2115 uz-Cyrl-UZ
845 2117 bn-BD
850 2128 mn-Mong-CN
86B 2155 quz-EC
C01 3073 ar-EG
C04 3076 zh-HK
C07 3079 de-AT
C09 3081 en-AU
C0C 3084 fr-CA
C1A 3098 sr-Cyrl-CS
C3B 3131 se-FI
C6B 3179 quz-PE
1001 4097 ar-LY
1004 4100 zh-SG
1007 4103 de-LU
1009 4105 en-CA
100A 4106 es-GT
100C 4108 fr-CH
101A 4122 hr-BA
103B 4155 smj-NO
1401 5121 ar-DZ
1404 5124 zh-MO
1407 5127 de-LI
1409 5129 en-NZ
140A 5130 es-CR
140C 5132 fr-LU
141A 5146 bs-Latn-BA
143B 5179 smj-SE
1801 6145 ar-MA
1809 6153 en-IE
180A 6154 es-PA
180C 6156 fr-MC
181A 6170 sr-Latn-BA
183B 6203 sma-NO
1C01 7169 ar-TN
1C09 7177 en-ZA
1C0A 7178 es-DO
1C1A 7194 sr-Cyrl-BA
1C3B 7227 sma-SE
2001 8193 ar-OM
2009 8201 en-JM
200A 8202 es-VE
201A 8218 bs-Cyrl-BA
203B 8251 sms-FI
2401 9217 ar-YE
2409 9225 en-029
240A 9226 es-CO
241A 9242 sr-Latn-RS
243B 9275 smn-FI
2801 10241 ar-SY
2809 10249 en-BZ
280A 10250 es-PE
281A 10266 sr-Cyrl-RS
2C01 11265 ar-JO
2C09 11273 en-TT
2C0A 11274 es-AR
2C1A 11290 sr-Latn-ME
3001 12289 ar-LB
3009 12297 en-ZW
300A 12298 es-EC
301A 12314 sr-Cyrl-ME
3401 13313 ar-KW
3409 13321 en-PH
340A 13322 es-CL
3801 14337 ar-AE
380A 14346 es-UY
3C01 15361 ar-BH
3C0A 15370 es-PY
4001 16385 ar-QA
4009 16393 en-IN
400A 16394 es-BO
4409 17417 en-MY
440A 17418 es-SV
4809 18441 en-SG
480A 18442 es-HN
4C0A 19466 es-NI
500A 20490 es-PR
540A 21514 es-US
201A 8218 bs-Cyrl-BA
141A 5146 bs-Latn-BA
281A 10266 sr-Cyrl-RS
241A 9242 sr-Latn-RS
243B 9275 smn-FI
82C 2092 az-Cyrl-AZ
203B 8251 sms-FI
814 2068 nn-NO
42C 1068 az-Latn-AZ
1C3B 7227 sma-SE
843 2115 uz-Cyrl-UZ
414 1044 nb-NO
428 1064 tg-Cyrl-TJ
82E 2094 dsb-DE
143B 5179 smj-SE
443 1091 uz-Latn-UZ
850 2128 mn-Mong-CN
1007F 65663 x-IV_mathan
10407 66567 de-DE_phoneb
1040E 66574 hu-HU_technl
10437 66615 ka-GE_modern
20804 133124 zh-CN_stroke
21004 135172 zh-SG_stroke
21404 136196 zh-MO_stroke
30404 197636 zh-TW_pronun
40404 263172 zh-TW_radstr
40411 263185 ja-JP_radstr
40C04 265220 zh-HK_radstr
41404 267268 zh-MO_radstr
       

发表评论