Change formating of Dates with a macro

B

blobb

Hi. I have a file that has a date of birth in the format of MM-DD-YYYY and I
would like to reformat it to YYYY-MM-DD.

An example of what the data looks like: <DoB>01-01-1990</DoB>

I was attempting to isolate the date and change the formating using this code:

With Selection.Find
.Text = "<DoB>"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
With Selection
.StartIsActive = False
.Extend Character:="<"
End With

Dim rdate As Range
Set rdate = Selection.Range
rdate = Format(rdate, "YYYY-MM-DD")

it doesn't appear to work. Could someone help? thanks!
 
J

Jay Freedman

You're doing this the hard way. Use a wildcard search
(http://www.gmayor.com/replace_using_wildcards.htm).

In the user interface Replace dialog, you can do this by clicking More and
checking "Use wildcards", then entering the following search and replace
strings:

Find What: (\<DoB\>)([0-9]{1,2})-([0-9]{1,2})-([0-9]{1,4})(\</DoB\>)
Replace With: \1\4-\2-\3\5

Then click the Replace All button.

As a macro, it would look like this:

Sub demo()
Dim myRg As Range
Set myRg = ActiveDocument.Range

With myRg.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "(\<DoB\>)([0-9]{1,2})-([0-9]{1,2})-([0-9]{1,4})(\</DoB\>)"
.Replacement.Text = "\1\4-\2-\3\5"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End Sub

I recommend using a Range object instead of the Selection for a couple of
reasons: It doesn't "inherit" any settings from the user's last search, it
doesn't leave behind any settings for the user's next search, and it doesn't
move the cursor.

Now, as for why your macro didn't work, it would start with the fact that you
never called the Find object's Execute method. What it would have done after you
inserted that, I can't say.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all
may benefit.
 
B

blobb

Wow that worked perfectly. Thank you very much!

Jay Freedman said:
You're doing this the hard way. Use a wildcard search
(http://www.gmayor.com/replace_using_wildcards.htm).

In the user interface Replace dialog, you can do this by clicking More and
checking "Use wildcards", then entering the following search and replace
strings:

Find What: (\<DoB\>)([0-9]{1,2})-([0-9]{1,2})-([0-9]{1,4})(\</DoB\>)
Replace With: \1\4-\2-\3\5

Then click the Replace All button.

As a macro, it would look like this:

Sub demo()
Dim myRg As Range
Set myRg = ActiveDocument.Range

With myRg.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "(\<DoB\>)([0-9]{1,2})-([0-9]{1,2})-([0-9]{1,4})(\</DoB\>)"
.Replacement.Text = "\1\4-\2-\3\5"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End Sub

I recommend using a Range object instead of the Selection for a couple of
reasons: It doesn't "inherit" any settings from the user's last search, it
doesn't leave behind any settings for the user's next search, and it doesn't
move the cursor.

Now, as for why your macro didn't work, it would start with the fact that you
never called the Find object's Execute method. What it would have done after you
inserted that, I can't say.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all
may benefit.

Hi. I have a file that has a date of birth in the format of MM-DD-YYYY and I
would like to reformat it to YYYY-MM-DD.

An example of what the data looks like: <DoB>01-01-1990</DoB>

I was attempting to isolate the date and change the formating using this code:

With Selection.Find
.Text = "<DoB>"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
With Selection
.StartIsActive = False
.Extend Character:="<"
End With

Dim rdate As Range
Set rdate = Selection.Range
rdate = Format(rdate, "YYYY-MM-DD")

it doesn't appear to work. Could someone help? thanks!
 

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