Using ordinals

V

VBANewbie

Hello, this is my first post to this group, and I have high hopes that
someone can help me out. I am creating a fairly simple template for my
company, and at one point in the template I wish to have the date
inserted automatically, but in this format:

It's Monday, the 16th of February.

Now it was fairly easy to insert the fields into the document and to
format them for the dates. The day of the week is formatted as DDDD,
the date is dd and the month is MMMM. This is great, but the result
shows as:

It's Monday, the 16 of February.

Well, I decided that I simply had to have the ordinals in place to
make the document more grammatically correct, and to make it flow more
smoothly.

I fiddled around for a while and finally came up with what I (humbly)
thought was the greatest solution in the world! Here is the code:

Sub AutoOpen()

Dim D1Append As String

ActiveDocument.Fields.Update

Select Case ActiveDocument.FormFields(2).Result
Case Is = 1 Or 21 Or 31
D1Append = "ST"
Case Is = 2 Or 22
D1Append = "ND"
Case Is = 3 Or 23
D1Append = "RD"
Case Else
D1Append = "TH"
End Select

ActiveDocument.Bookmarks("Date").Select

With Selection
.InsertAfter D1Append
.Collapse Direction:=wdCollapseEnd
.ExtendMode = True
.MoveLeft Unit:=wdCharacter, Count:=2
.Font.Superscript = True
.Collapse Direction:=wdCollapseEnd
End With

End Sub

As I had created this macro around the middle of January, I anxiously
awaited the 21st, 22nd, and 23rd of the month to see if my creation
did what I expected. Sure enough, it worked! A cursory and
self-assured glance at my handiwork on the 31st simply served to
inflate my ego even more. However, on the following day, Sunday the
1st of February, imagine my dismay when I observed the results:

Sunday, the 1th of February

and the following day:

Monday, the 2th of February

and yet again:

Tuesday the 3th of February

Three quick, successive blows to my over-inflated ego to bring me back
down to earth and to realize that I really don't know a damn thing!

After staring at this a while, I thought "Aha! The date in the
template is formatted as "dd", so the result in that field will never
be 1 or 2 or 3, it will always be 01 or 02 or 03". So I went back into
my code and tried to pre pend a zero to my dates, but apparently VB
doesn't like leading zeros, since the program stripped them away as
soon as I typed them.

Then I thought, "Hey Dodo, there's an easier way - just go into the
template and change the formatting of the field from 'dd' to 'd'."
That's exactly what I did, believing that it was a brilliant and
rather insightful solution to my problem . . . except that it didn't
work.

And thus, my quandary. I would greatly appreciate hearing from anyone
willing to put me out of my VBA misery. Thanks in advance.

Anyone wishing to reply by email, please remove the words in UPPERCASE
from the email address.

Damelon
 
P

Peter Hewett

Hi Damelon

You're entering the day part of your date manually into a FormField, Yes?
So I'm presuming that the user of your template entered the day number when
the document was created???

I'm not sure I'm getting it all, because your doing a:
ActiveDocument.Fields.Update
and then referring to a FormField. Obviously this statement updates some
other (non FormField) fields in your document???

Ok, the problem is that your case statement is incorrect. Essentially it's
always dropping through to the Else clause so everthing ends up with a
"TH"!! try the following:

Select Case CLng(ActiveDocument.FormFields(2).Result)
Case 1, 21, 31
D1Append = "ST"
Case 2, 22
D1Append = "ND"
Case 3, 23
D1Append = "RD"
Case Else
D1Append = "TH"
End Select

If the above code generates an error message the the 2nd FormField in your
document is not returning a number.

If your date is comming from a Word Date field, then you probably don't
need any code at all because Word can display ordinal dates:

{ DATE \@ "d" \* \ordinal }

HTH + Cheers - Peter
 
V

VBANewbie

Thanks Peter,

I truly appreciate you taking the time to set me straight - I have
tried the code you provided and it works perfectly - I just don't
understand how I could have been so far off base with the CASE
statement. Thanks again.

Damelon
 
M

macropod

Hi,
Another way to do this, without macros, is to use a compound Word field as
follows:
{QUOTE{DATE \@ dddd}" the "{DATE \@ d \*Ordinal}" of "{DATE \@ "MMMM,
YYYY"}}
Depending what you're trying to achieve, you could change the DATE
references to CREATEDATE, SAVEDATE or PRINTDATE.

Cheers
PS: The field braces are entered in pairs via Ctrl-F9.
 
C

Charles Kenyon

Consider putting a simple CREATEDATE field in your template instead of using
code for this. That date will show the date a new document is created based
on the template and will remain fixed. If your document is not protected for
forms, it can be changed by the user if needed.
--

Charles Kenyon

See the MVP FAQ: <URL: http://www.mvps.org/word/> which is awesome!
--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 

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