Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Star Lounger
    Join Date
    Aug 2015
    Posts
    98
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Search for date ranges or roman numerals in Word 2010

    Hi,

    I have tried searching through the many posts in the forum but cannot find what I am looking for so my apologies if the following is already listed somewhere.

    I want to insert non-breaking spaces wherever a date is written in my documents e.g. 1 January 2003, 1 October 2010, 11 December 2015 or wherever a Roman numeral is used e.g. Part IV of the Landlord and Tenant Act 1985. Does anyone know what the wildcard range would be for either of these. I would like to add this to a macro I currently use to apply house style to my documents but my coding isn't great.

    My macro does insert non-breaking spaces wherever there is a digit but only before the digit which is great when there are references to clause 1, paragraph 2, part 8, schedule 10, Act 1985 etc. Currently for dates I have to keep manually changing [non-breaking space]1[space]January[non-breaking space]2003

    How can I insert non-breaking spaces for dates, is there a wildcard search I can perform and then add this to my macro.

    Many thanks
    Shelley

  2. #2
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,433
    Thanks
    371
    Thanked 1,456 Times in 1,325 Posts
    Shelly,

    What you need here is a Regular Expression (RegEx).

    To do the dates you would enter the following in the Find & Replace dialog:

    Find What: ([0-9]{1,2}) (<[ADFJMNOS]*>) ([0-9]{4})
    Mind the spaces in the above!
    Replace With: \1^s\2^s\3
    Check: Use Wildcards
    ShellyF&R.JPG
    Click: Replace All
    F&$msg.JPG
    Results, note the symbols for Non-Breaking Spaces.
    S&RResults.JPG

    As for the Roman Numerals I'm not sure how many NBS you want inserted, e.g. just after the RN or in the whole citation (which would be very complicated).

    HTH
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  3. #3
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,433
    Thanks
    371
    Thanked 1,456 Times in 1,325 Posts
    Shelly,

    Here's an explanation of post #2.

    Find What:

    This Regular Expression has 3 parts, delineated by parens ( ).
    Part 1: ([0-9]{1,2}) finds any 1 or 2 digit numbers 0-999.
    Part 2: (<[ADFJMNOS]*>) finds words starting w/one of the Capital Letters (e.g. the letters of the Months of the year) any number of characters following delimited by a space on either side (i.e. a whole word).
    Part 3: ([0-9]{4}) finds a 4 digit number.

    The Replace With uses the 3 parts above \1 \2 \3 and inserts the wildcard for non-breaking space (^s), e.g.
    \1^s\2^s\3

    HTH
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  4. #4
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    You can search for roman numerals with a wildcard Find: <[IVXLCDM]@>
    If you want to replace the space before with a non-breaking space, you could use:
    Find = ^32([IVXLCDM]@>)
    Replace = ^0160\1
    A potential problem, though, is that it will also replace the space before the personal pronoun 'I' with a non-breaking space. Likewise with other capitalised words link DIM, LID, MID, MIDI, etc.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  5. #5
    Star Lounger
    Join Date
    Aug 2015
    Posts
    98
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Hi, many thanks for taking the time on this for me, it works on my documents.

    If I wanted to change this part (<[ADFJMNOS]*>) to include all letters in uppercase or lowercase of the alphabet would it be (<[Aa-Zz]*>)

    Regards, Shelley
    Last edited by Lady-Laughsalot; 2015-12-14 at 04:42.

  6. #6
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Quote Originally Posted by Lady-Laughsalot View Post
    If I wanted to change this part (<[ADFJMNOS]*>) to include all letters in uppercase or lowercase of the alphabet would it be (<[Aa-Zz]*>)
    To find any mix up upper-case & lower-case letters, you could use (<[A-Za-z]*>) but, with RG's Find expression, that is liable to result in false matches with any number followed by a word then a number of 4-digits or more. I'd suggest using:
    Find = (<[0-9]{1,2}) ([JFMASOND][!dfjkqwxz0-9]{2,8}) ([0-9]{4}>)
    which will find dates that include any proper-case month name, whether abbreviated or not.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  7. The Following User Says Thank You to macropod For This Useful Post:

    Charles Kenyon (2015-12-16)

  8. #7
    Star Lounger
    Join Date
    Aug 2015
    Posts
    98
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Thank you - would this also find any digits before lowercase/initial capital words such as 10 days, 10 minutes, 10 years, 10 months, 10 Business Days, 10 Working Days etc. and insert a non-breaking space. Thanks Shelley

  9. #8
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    No. All you've asked for - and all you've been provided with - are expressions for finding dates and roman numerals; expressions such as 10 days, 10 minutes, 10 years, 10 months, 10 Business Days, 10 Working Days, etc. are neither. If you want to replace the ordinary spaces after all numbers with non-breaking spaces you could use a wildcard Find/Replace with:
    Find = ([0-9])^32
    Replace = \1^s
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  10. #9
    Star Lounger
    Join Date
    Aug 2015
    Posts
    98
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Non-breaking spaces

    non-breaking spaces.PNG

    Hi, yes my apologies for not being specific before. The original post was for non-breaking spaces with dates but then I realized I need it to go further with all digits in my documents. I currently use the macro below to insert non-breaking spaces before digits, e.g. clause 1, paragraph 2 which works great but I want the macro to go a step further and add in your suggestion of find ([0-9])^32 replace with \1^s and also for dates
    (<[0-9]{1,2}) ([JFMASOND][!dfjkqwxz0-9]{2,8}) ([0-9]{4}>) replace with \1^s\2^s\3 but I'm having problems in doing this. I'm also not sure how to deal with the problem in getting the macro to insert non-breaking spaces either before OR after the digit (I've attached an example of where non-breaking spaces should be inserted). I am probably asking Word 2010 to be far too clever for what it can do.
    Code:
    Sub DPU_CleanUpDoc()
    '
    ' DPU_CleanUpDoc Macro
    '
    '
         Dim aField As Field, Rng As Range
     For Each aField In ActiveDocument.Fields
        If aField.Type = wdFieldRef Then
          Set Rng = aField.Result
          If Rng.Previous.Characters(1) = " " Then
            Rng.Previous.Characters(1) = Chr(160)
          End If
        End If
      Next
      With Selection.Find
     .ClearFormatting
      .Replacement.ClearFormatting
      .Text = " ([0-9])"
      .Replacement.Text = "^s\1"
      .Forward = True
      .Wrap = wdFindContinue
      .Format = False
      .MatchCase = False
      .MatchWholeWord = False
      .MatchAllWordForms = False
      .MatchSoundsLike = False
      .MatchWildcards = True
      .Execute Replace:=wdReplaceAll
    End With
    Last edited by RetiredGeek; 2015-12-16 at 16:59. Reason: Added Code Tags

  11. #10
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Do you really want non-breaking spaces before the 'day' part of a date and after the year?

    Also, when posting code, please use the code tags. They're on the 'Go Advanced' tab at the bottom of this screen.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  12. #11
    Star Lounger
    Join Date
    Aug 2015
    Posts
    98
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Hi Macropod, no I don't want non-breaking spaces before the day part of a date or after the year - unfortunately, the macro I have at the moment only puts non-breaking spaces 'before' all digits - haven't quite worked out how to instruct the macro to insert NBS for dates e.g. 17[NBS]December[NBS]2015, then only insert NBS in between a word and a digit e.g.
    clause[NBS]1 or paragraph[NBS]1 and then only insert NBS after a digit and before a word e.g. 10[NBS]years. Unfortunately, if I run my find and replace macro, your find and replace suggestion and RG's date find and replace I end up with non-breaking spaces where I don't want them.

    My apologies for not inserting my macro coding correctly, I will remember to do this in the future.
    Many thanks, Shelley

  13. #12
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    It's by no means clear what you want. Non-breaking spaces within dates are easy enough. Non-breaking spaces before and/or after other numbers are also easy enough. But it seems you might also want some numbers to only have non-breaking spaces before and other numbers to only have non-breaking spaces after, which would require the macro to play a guessing game. Leaving aside the last of these, try:
    Code:
    Sub DPU_CleanUpDoc()
    Dim Fld As Field, Rng As Range
    With ActiveDocument
      For Each Fld In .Fields
        If Fld.Type = wdFieldRef Then
          Set Rng = Fld.Result.Previous.Characters(1)
          If Rng.Text = " " Then Rng.Text = Chr(160)
        End If
      Next
      With .Range.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Format = False
        .Forward = True
        .Wrap = wdFindContinue
        .MatchWildcards = True
        .Text = "(<[0-9]{1,2}) ([JFMASOND][anuryebchpilgstmov]{2,8}) ([0-9]{4}>)"
        .Replacement.Text = "\1^s\2^s\3"
        .Execute Replace:=wdReplaceAll
        .Text = " (<[0-9]{1,}>) "
        .Replacement.Text = "^s\1^s"
        .Execute Replace:=wdReplaceAll
        .Text = " (<[0-9]{1,}>[!^s])"
        .Replacement.Text = "^s\1"
        .Execute Replace:=wdReplaceAll
        .Text = "([!^s]<[0-9]{1,}>) "
        .Replacement.Text = "\1^s"
        .Execute Replace:=wdReplaceAll
      End With
    End With
    End Sub
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  14. #13
    Star Lounger
    Join Date
    Aug 2015
    Posts
    98
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Thanks Macropod, I have run your macro and it works, thank you so much. I am now trying to put another macro to run after your macro just to tidy up certain key words with digits within the document, these mainly being minute, hour, day, week, month, year, working day, business day, Act e.g. [NBS]3[NBS]months or [NBS]1[NBS]Working Day. To do this I am using Find & Replace - Find: ^s([0-9])^smonth replace with: [space]\1^smonth - this is working but it does not pick up double digits e.g. 10 months. I've tried in the replace with to run a search [space]\1\2^smonth but it keeps saying 'out of range'.

    My job all day is to amend/correct typed documents and put into company house style so these little macros really do save me a lot of time and I am incredibly grateful for the help you have given me.

    Regards
    Shelley

    p.s I've worked out I need to put a double [0-9][0-9] in the find box
    Last edited by Lady-Laughsalot; 2015-12-17 at 09:22.

  15. #14
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,433
    Thanks
    371
    Thanked 1,456 Times in 1,325 Posts
    Shelley,

    Actually what you want is: [0-9]{1,2} vs [0-9][0-9].

    HTH
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  16. #15
    Star Lounger
    Join Date
    Aug 2015
    Posts
    98
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Thanks RG, my macro was starting to look far too long - I presume [0-9]{1,2} finds any one or two digits - thanks. Shelley

Page 1 of 2 12 LastLast

Posting Permissions

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