View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
NickHK NickHK is offline
external usenet poster
 
Posts: 4,391
Default Format an Excel Column in the window's short date format.

Jim,
This works for me in VBA. Maybe your UnicodeBString code is not doing what
you expect ?

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

Private Sub CommandButton1_Click()
Dim Buffer As String, Ret As Long
Dim GetInfo As String

Const LOCALE_USER_DEFAULT = &H400
Const LOCALE_SSHORTDATE As Long = &H1F

Buffer = String$(256, 0)

Ret = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SSHORTDATE, StrPtr(Buffer),
Len(Buffer))

If Ret 0 Then
GetInfo = Left$(Buffer, Ret - 1)
Else
GetInfo = ""
Exit Sub
End If

MsgBox GetInfo

Range("A1").Value = CLng(Now())
Range("A1").NumberFormat = GetInfo
Debug.Print Range("A1").Text

End Sub



"jim kane" wrote in message
...
I'm trying to use com to format an Excel column as a date in window's

short
date format.
It works for most regional settings but not Norwegian(Bokmal).
To test this I set my regional settings to Norwegian(Bokmal) in control
pannel. ( I have US windows xp and excel 2003 )
I call GetLocaleInfoW() to get the regional window's short date setting

and
it returns 'dd.MM.yyyy' which is correct I believe.
I then set the NumberFormat Property of the Excel Column object to
'dd.MM.yyyy' by converting the unicode received from GetLocaleInfoW to a
bstring.
To my surprise a date such as 12 January 2006 appears in that column as
12.01.yyyy instead of 12.01.2006 as I expected.
using the NumberFormatLocal format produced the same result.
It appears that when regional settings are set to Norwegian(Bokmal) 'yyyy'
is not the correct date formating code for the year portion of the date.

If anyone can point me in the right direction on how to format a column

for
the window's short date format for various regional settings, I'd greatly
appreciate it. using the date string from GetLocaleInfoW() isnt a useful
approach.
I was hoping there was a format string that allowed setting the date

format
for a column to the window's short date format without having to construct

a
string such as 'dd.MM.yyyy' which appears problematic.

Jim Kane