Determining regional setting in VBA code-VBForums
Results 1 to 5 of 5

Thread: Determining regional setting in VBA code

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    5

    Determining regional setting in VBA code

    How do you do it?

    In other words, at run-time, I want to determine the system regional settings and be able to test against the returned info.

    Of course, using something like MonthName(1), it would give me either an English name for January, or the word in some other language. But there's got to be a better way...

    Any suggestions?

  2. #2

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    5
    Well, I found this code:

    VB Code:
    1. 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
    2.  
    3. Public Function ReadLocaleInfo(ByVal lInfo As Long) As String
    4.    
    5.     Dim sBuffer As String
    6.     Dim rv As String
    7.    
    8.     sBuffer = String$(256, 0)
    9.     rv = GetLocaleInfo(LOCALE_USER_DEFAULT, lInfo, sBuffer, Len(sBuffer))
    10.    
    11.     If rv > 0 Then
    12.         ReadLocaleInfo = Left$(sBuffer, rv - 1)
    13.     Else
    14.         'MsgBox "Not found"
    15.         ReadLocaleInfo = ""
    16.     End If
    17. End Function

    I've tried it with the following inputs:

    VB Code:
    1. MsgBox ("UserDefault: " & ReadLocaleInfo(LOCALE_USER_DEFAULT))
    2. MsgBox ("SDecimal: " & ReadLocaleInfo(LOCALE_SDECIMAL))
    3. MsgBox ("ILDate: " & ReadLocaleInfo(LOCALE_ILDATE))
    4. MsgBox ("Country: " & ReadLocaleInfo(LOCALE_ICOUNTRY))

    ...and in each case, it returned an empty string.

    Any ideas?

  3. #3
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,088
    I have this example from allapi.net which is where you may have
    got the example from and they use different const.'s
    This works for me but the UserDefault.
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function GetLocaleInfo Lib "kernel32" Alias "GetLocaleInfoA" (ByVal Locale As Long, _
    4. ByVal LCType As Long, ByVal lpLCData As String, ByVal cchData As Long) As Long
    5.  
    6. Private Const LOCALE_USER_DEFAULT = &H400
    7. Private Const LOCALE_SDECIMAL As Long = &HE
    8. Private Const LOCALE_ILDATE As Long = &H22
    9. Private Const LOCALE_ICOUNTRY As Long = &H5
    10. Private Const LOCALE_SENGCOUNTRY = &H1002 ' English name of country
    11. Private Const LOCALE_SENGLANGUAGE = &H1001  ' English name of language
    12. Private Const LOCALE_SNATIVELANGNAME = &H4  ' native name of language
    13. Private Const LOCALE_SNATIVECTRYNAME = &H8  ' native name of country
    14.  
    15. Public Function GetInfo(ByVal lInfo As Long) As String
    16.     Dim Buffer As String
    17.     Dim Ret As String
    18.     Buffer = String$(256, 0)
    19.     Ret = GetLocaleInfo(LOCALE_USER_DEFAULT, lInfo, Buffer, Len(Buffer))
    20.     If Ret > 0 Then
    21.         GetInfo = Left$(Buffer, Ret - 1)
    22.     Else
    23.         GetInfo = ""
    24.     End If
    25. End Function
    26.  
    27. Private Sub Form_Load()
    28.     MsgBox "You live in " & GetInfo(LOCALE_SENGCOUNTRY) & _
    29.     " (" & GetInfo(LOCALE_SNATIVECTRYNAME) & ")," & vbCrLf & "and you speak " & GetInfo(LOCALE_SENGLANGUAGE) & _
    30.     " (" & GetInfo(LOCALE_SNATIVELANGNAME) & ").", vbInformation 'WORKS CORRECTLY
    31.     MsgBox ("UserDefault: " & GetInfo(LOCALE_USER_DEFAULT)) 'STILL EMPTY STRING
    32.     MsgBox ("SDecimal: " & GetInfo(LOCALE_SDECIMAL)) 'RETURNS "."
    33.     MsgBox ("ILDate: " & GetInfo(LOCALE_ILDATE))     'RETURNS "0"
    34.     MsgBox ("Country: " & GetInfo(LOCALE_ICOUNTRY))  'RETURNS "1"
    35. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Star Wars Gangsta Rap Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel Core 2 Extreme Ed., 2 WD Raptor 10K RPM 300 GB HDs, 2 GBs DDR2 667 MHz RAM, 2 Viewsonic 24" LCDs, Windows 7 SP-1/Windows 8.1, Office 2010, VS 2013

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    5
    Thanks for the response...

    As it turns out, if I just use the hex codes, things work fine.

  5. #5
    Fanatic Member ZeBula8's Avatar
    Join Date
    Oct 2002
    Posts
    548
    Application.International(xlCountrySetting)

    this will return the regional country settings and you will be able to set your application accordingly.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.