Results 1 to 10 of 10
  1. #1
    New Lounger
    Join Date
    Feb 2001
    Location
    NY, New York, USA
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Macro to delete date (Word 2003)

    Hello all,

    I need a macro to delete a date that is just text (NOT in a field) in the form Friday, Jul 27, 2004. Any ideas?

    Thanks,

    Paul

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Macro to delete date (Word 2003)

    Such a macro could be written, but it would be cumbersome, and probably slow in large documents. It would be a lot easier if the date were bookmarked. Is this for existing documents, or for documents yet to be created? And what do you want to accomplish with this macro?

  3. #3
    New Lounger
    Join Date
    Feb 2001
    Location
    NY, New York, USA
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Macro to delete date (Word 2003)

    I am bringing in text created in another program and need to clean it up before it reposting it in yet a third program!

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Macro to delete date (Word 2003)

    Will there only be one occurrence of such a date in the text, or can there be more? If the latter, should all of them be deleted, or only the first occurrence, or ...?

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Macro to delete date (Word 2003)

    Andrew,

    Very clever! It almost works in a test I did, but fails for April, August, September and October. Adding gpt to [JFMAMJSONDanuryfeblmjsohdvc] seems to fix it.

  6. #6
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Macro to delete date (Word 2003)

    Hi Paul,

    Try this (on a copy of your document):

    <pre>Sub DeleteDates()
    Dim doc As Document
    Dim fld As Field

    Set doc = ActiveDocument
    For Each fld In doc.Fields
    fld.Select
    Selection.Font.Hidden = True
    Next fld
    With doc.Range.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .MatchWildcards = True
    .MatchWholeWord = True
    .MatchCase = False
    .Format = False
    .Text = "<[MTWFSondayueshrit]{6,9}, " & _
    "[JFMASONDanuryebchpilgstmov]{3,12} [0-9]{1,2}, " & _
    "[12][0-9]{3}>"
    .Replacement.Text = vbNullString
    .Execute Replace:=wdReplaceAll
    End With

    For Each fld In doc.Fields
    fld.Select
    Selection.Font.Hidden = False
    Next fld
    End Sub
    </pre>


    Be careful to replicate the spacing in the search string exactly.

  7. #7
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Macro to delete date (Word 2003)

    <P ID="edit" class=small>(Edited by Andrew77 on 25-Jul-04 16:05. )</P>Hi Hans,

    I didn't test it on every day/month combo, and I'm not surprised it needed some tweaking. Perhaps if I'd methodically worked out the appropriate letters rather than trying to spell each month and day in my head as I typed, it would have worked a little better <img src=/S/grin.gif border=0 alt=grin width=15 height=15>.

    (I've cleaned up the search string in the original post to remove duplicate letters, as well as adding the letters indicated by Hans).

    Thanks!

  8. #8
    5 Star Lounger kmurdock's Avatar
    Join Date
    Feb 2003
    Location
    Pacific Grove, California, USA
    Posts
    716
    Thanks
    10
    Thanked 34 Times in 28 Posts

    Re: Macro to delete date (Word 2003)

    OK. But why?

    I can see bits of the months sprinkled in the code, but can you 'splain it for those of us (me, for example <img src=/S/grin.gif border=0 alt=grin width=15 height=15> ) who can't tell why it's clever? I would love to know how you parsed this. Please?

    Thanks,
    Kim

  9. #9
    5 Star Lounger kmurdock's Avatar
    Join Date
    Feb 2003
    Location
    Pacific Grove, California, USA
    Posts
    716
    Thanks
    10
    Thanked 34 Times in 28 Posts

    Re: Macro to delete date (Word 2003)

    Wow, Andrew -- Hans was right. Very, very clever.

    I've never had to use Find with such complexity. This is such good info to have for problem-solving. Thanks!

    Kim

  10. #10
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Macro to delete date (Word 2003)

    Hi Kim,

    The macro uses several of Word's wildcard features:

    [bsc]at
    matches "bat", "sat", and "cat" (Any one of the characters in brackets, followed by "at")

    to{1,2}n
    matches "to" and "toon" (a "t", followed by 1 or 2 "o", followed by "n")

    <room
    matches "room" but not "broom" ("room" only if it's at the beginning of a word)

    Now let's step through the wildcard combinations in the macro, which needs to match dates like "Sunday, July 25, 2004".
    First, we need to match days of the week. They all start with M,T,W, F or S, followed by 5 to 8 of the following letters: "ondayueshrit". So we can put all of those letters in between brackets, and ask word to find them when any 6 to 9 of them occur in a row, in any order. That may, of course, match words that aren't days of the week, but when combined with the other criteria, it's pretty unlikely there will be any false matches.
    <pre>[MTWFSondayueshrit]{6,9}</pre>


    Next, we need to match the months. We can use the same general plan. They all start with J,F,M,A,S,O,N, or D, followed by 2 to 11 of the following letters: "anuryebchpilgstmov". So we can put all those letters in between brackets, and ask Word to find them when any 3 to 12 of them occur in a row, in any order:
    <pre>[JFMASONDanuryebchpilgstmov]{3,12}</pre>


    Now on to the dates. Since there are so many other criteria in this match, I'm not too particular about the date numbers. I just ask Word to find one or two digits in a row. Technically, that means that in the full pattern, a nonsense date like "Monday, June 99, 2003" would match, but I'm assuming there aren't dates like that in the document.
    <pre>[0-9]{1,2}</pre>


    Next, the years. For that, I'm assuming that all the dates will be between the years 1000-2999. So I just need to ask Word to find a 1 or a 2, followed by any three numbers:
    <pre>[12][0-9]{3}</pre>


    So a generic expression of the pattern, with commas, is:
    (Day), (Month) (Date), (Year)

    And just to be extra sure we're not matching any more than needed, the "<" and ">" characters tell Word to only match if the pattern starts at the beginning of a word, and ends at the end of one. So something (admittedly nonsense) like "FooMonday, June 19, 193390210" wouldn't match the pattern.

    Bringing it all together, the final pattern is:
    <pre><[MTWFSondayueshrit]{6,9}, [JFMASONDanuryebchpilgstmov]{3,12} [0-9]{1,2}, [12][0-9]{3}></pre>

    I encourage you to experiment with each part of the pattern from within Word, using the Find What dialog box, with Wildcards turned on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •