VBA Date Format to Word Doc Date Format

T

Terry

I am passing a date as a string from VBA (Access 2007) and placing it at a
Bookmark in a Word document.

The date is passed as mmmm dd yyyy and shows as August 11 2009.

Is there a way to have Word accept the date and apply a format such as
August 11th 2009? or have I to do the work before passing the string.

Regards
 
G

Graham Mayor

As far as I am aware there is no simple way to add ordinal formats in Word
vba. If the date is in the format MMMM dd yyyy then you can format the date
in your macro and paste the result. The code in Word vba could be something
like: Where sDate is the date you wish to format.

Dim sDate As String
Dim orDStr As String
Dim pStrDay As Variant
sDate = "August 11 2009"
pStrDay = Split(sDate, " ")
Select Case pStrDay(1)

Case "01", "21", "31"
orDStr = pStrDay(0) & Chr(160) _
& pStrDay(1) & "st" & Chr(160) & _
pStrDay(2)

Case "02", "22"
orDStr = pStrDay(0) & Chr(160) _
& pStrDay(1) & "nd" & Chr(160) & _
pStrDay(2)

Case "03", "23"
orDStr = pStrDay(0) & Chr(160) _
& pStrDay(1) & "rd" & Chr(160) & _
pStrDay(2)

Case Else
orDStr = pStrDay(0) & Chr(160) _
& pStrDay(1) & "th" & Chr(160) & _
pStrDay(2)

End Select
Selection.TypeText orDStr


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
T

Terry

Hi Graham,
Thanks for the code. I had thought I may need to format it first before
passing the string.
Terry
 

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