Change the dateformat through VBA

F

fam.hougaard

I'm using af danish version of Windows XP and Word 2003 and of course
have the dateformat in the danish format like : "27. januar 2009" I
use Today = FormatDateTime(Date, 1) in VBA to retrieve the current
date.

I would like to be able to change this to the english format "27
January, 2009" using VBA in Word. I have tried Today = Format(Date,
"dd mmmm, yyyy") but the months is of course still in danish "Januar".

Is it possible to change it to "January" using VBA?

I know i can change the dateformat in the control panel, regional
settings but thats not the solution.
 
D

Doug Robbins - Word MVP

Use

Dim Month As Variant
Month =
Split("January/February/March/April/May/June/July/August/September/October/November/December",
"/")
MsgBox "Today is " & Format(Date, "d") & " " & Month(Format(Date, "m") - 1)
& " " & Format(Date, "yyyy")


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
F

fam.hougaard

Use

Dim Month As Variant
Month =
Split("January/February/March/April/May/June/July/August/September/October/­November/December",
"/")
MsgBox "Today is " & Format(Date, "d") & " " & Month(Format(Date, "m") - 1)
& " " & Format(Date, "yyyy")

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP









- Vis tekst i anførselstegn -

Works like a charm - thanks!
 
T

Tony Jollans

Although VBA won't tell you directly, Word knows the names of the months
without you having to hard code them.

For a more general way of approaching this, try:

Dim TheDate As String
Dim TheFormat As String

' Put your desired values in the three lines below
TheFormat = "\@ ""dddd, dd MMMM yyyy"""
TheDate = CStr(Date)
Selection.LanguageID = wdEnglishUK

With Selection.Fields.Add(Range:=Selection.Range, _
Type:=wdFieldQuote, _
Text:="""" & TheDate & """ " & TheFormat, _
PreserveFormatting:=False)
.Update
MsgBox .Result
End With

ActiveDocument.Undo 3

This just gives a MsgBox with the date in it, but you can take the string
and do as you will with it. You can use whatever date, format, and language
you wish.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top