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
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