Word umm auto formatting? help?

For general computer discussion & help, come here

Moderators: Bakhtosh, EvilHomer3k

Post Reply
Cortilian
Posts: 1590
Joined: Fri Jan 14, 2005 10:30 am

Word umm auto formatting? help?

Post by Cortilian »

I don't even really know the right word to describe what my issue is here or I'm sure Google would tell me but here goes:

I have a word document that has a date field that is filled by a token [DATE_RECEIVED]. This field populates in this format mm/dd/yyyy. This field populates in 2 places in the document, like this:

Code: Select all

Received Date: [DATE_RECEIVED]

Dear Mr. Jones:

I have received your letter on [DATE_RECEIVED] yadda yadda yadda
So, that has worked great for me for years. Now the bosses have decided that they do not like that the 2nd date is also in mm/dd/yyyy format. That want the 2nd instance of the date to be Month dd, yyyy (January 1, 1970) but want the 1st one to stay mm/dd/yyyy.

Anyone know of a way for me to have the 2nd instance of that field converted into Month Day, Year?

Thanks.
User avatar
gilraen
Posts: 4319
Joined: Wed Sep 04, 2013 7:45 pm
Location: Broomfield, CO

Re: Word umm auto formatting? help?

Post by gilraen »

The "[DATE_RECEIVED]" is not a built-in Word function - how does it populate? Do you have to type in the date and then it auto-formats? Theoretically you can change the format manually, by highlighting the date text and going to Insert -> Date & Time.
Cortilian
Posts: 1590
Joined: Fri Jan 14, 2005 10:30 am

Re: Word umm auto formatting? help?

Post by Cortilian »

gilraen wrote:The "[DATE_RECEIVED]" is not a built-in Word function - how does it populate? Do you have to type in the date and then it auto-formats? Theoretically you can change the format manually, by highlighting the date text and going to Insert -> Date & Time.
It is a token populated by a 3rd program. It populates in the mm/dd/yyyy format by default. I have tried using field switches like \@ "MMMM d, yyyy" but it doesn't recognize the date as a field. I can do it all manually but I do like 50 of these a week and for the past 7 years they have been fine but now they want that one format changed.
User avatar
gilraen
Posts: 4319
Joined: Wed Sep 04, 2013 7:45 pm
Location: Broomfield, CO

Re: Word umm auto formatting? help?

Post by gilraen »

If you create a VBA macro to format the date, and then save your document as a macro-enabled template, AND set the macro to run automatically when you create a new document from this template...it could work.

I tested it but the only way I could figure out to get it to only format the 2nd date occurrence was to insert a bookmark called ReceivedDate (highlight the entire date text that will need to be replaced, go to Insert -> Bookmarks).

I also could only get it to work automatically when creating a new document from the template (AutoNew). Not the AutoExec version (opening existing document), because it tries to load the macro before the bookmark is loaded. Of course, you can always put it on a button and click it to run manually.

Code: Select all

Sub AutoNew()
    Selection.GoTo What:=wdGoToBookmark, Name:="ReceivedDate"
    With Selection.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "([0-9]{1,2})[/]([0-9]{1,2})[/]([0-9]{4})"
        .Format = True
        .Forward = True
        .MatchWildcards = True
    End With

    Selection.Find.Execute Replace:=wdReplaceNone

    Selection.Text = Format(Selection.Text, "mmmm d, yyyy")
    Selection.Collapse wdCollapseEnd
End Sub
Or it might be easier to find whoever is responsible for that 3rd-party program and have them create a different token with the new date formatting ;)
Cortilian
Posts: 1590
Joined: Fri Jan 14, 2005 10:30 am

Re: Word umm auto formatting? help?

Post by Cortilian »

Well, hot damn. That actually worked. It erases the bookmark when inserting the reformatted date but that was very useful. Now to insert the 20 bookmarks, edit the macro and run a big test. Thank you.
Cortilian
Posts: 1590
Joined: Fri Jan 14, 2005 10:30 am

Re: Word umm auto formatting? help?

Post by Cortilian »

It does work. I have to run it twice because it always skips the 1st date it needs to reformat. I run it twice and it then catches that bookmark, formats that one, and errors out because the other bookmarks are gone but that's no big deal. Here is the code I used. Thanks again.

Code: Select all

Sub datefix()
'
' datefix Macro
'
Selection.GoTo What:=wdGoToBookmark, Name:="DATE_RECEIVED"
    With Selection.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "([0-9]{1,2})[/]([0-9]{1,2})[/]([0-9]{4})"
        .Format = True
        .Forward = True
        .MatchWildcards = True
    End With

    Selection.Find.Execute Replace:=wdReplaceNone

    Selection.Text = Format(Selection.Text, "mmmm d, yyyy")
    Selection.Collapse wdCollapseEnd
'
Selection.GoTo What:=wdGoToBookmark, Name:="DATE_CONTACTED"
    With Selection.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "([0-9]{1,2})[/]([0-9]{1,2})[/]([0-9]{4})"
        .Format = True
        .Forward = True
        .MatchWildcards = True
    End With

    Selection.Find.Execute Replace:=wdReplaceNone

    Selection.Text = Format(Selection.Text, "mmmm d, yyyy")
    Selection.Collapse wdCollapseEnd
    '
Selection.GoTo What:=wdGoToBookmark, Name:="DATE_INSPECTED"
    With Selection.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "([0-9]{1,2})[/]([0-9]{1,2})[/]([0-9]{4})"
        .Format = True
        .Forward = True
        .MatchWildcards = True
    End With

    Selection.Find.Execute Replace:=wdReplaceNone

    Selection.Text = Format(Selection.Text, "mmmm d, yyyy")
    Selection.Collapse wdCollapseEnd
'

End Sub
User avatar
gilraen
Posts: 4319
Joined: Wed Sep 04, 2013 7:45 pm
Location: Broomfield, CO

Re: Word umm auto formatting? help?

Post by gilraen »

I'm glad you got it to work for you :)
Post Reply