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

    Help with non-breaking spaces

    Hi Macropod

    You very kindly helped me with some coding a while ago re clean up macro, but this post is now marked as solved. I just have a query to some of the coding as I can't remember what it does. I think it is supposed to delete a non-breaking space when there is one either side of a digit, if it is I don't think it is working as it still inserts them either side.

    Code:
     For i = 0 To UBound(ArrFnd)
          .Text = "^s([0-9]{1,}^s" & ArrFnd(i) & ")"
          .Replacement.Text = " \1"
          .Execute Replace:=wdReplaceAll
    Many thanks, Shelley

    whole code as follows:

    Code:
    Sub DPU_HouseStyleFormatCleanUp()
    Application.ScreenUpdating = False
    Dim Fld As Field, Rng As Range, i As Long, ArrFnd
    ArrFnd = Array("[Mm]inute", "[Hh]our", "[Dd]ay", "[Ww]eek", "[Mm]onth", "[Yy]ear", "[Ww]orking", "[Bb]usiness", "Act")
    With ActiveDocument
      'ensure spaces before cross-references are non-breaking
        For Each Fld In .Fields
        If Fld.Type = wdFieldRef Then
          If Not Fld.Result.Previous Is Nothing Then
            Set Rng = Fld.Result.Previous.Characters(1)
            Rng.Text = Replace(Rng.Text, " ", Chr(160))
          End If
        End If
      Next
      With .Range.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Format = False
        .Forward = True
        .Wrap = wdFindContinue
        .MatchWildcards = True
        'Ensure spaces within dates are non-breaking
        .Text = "(<[0-9]{1,2})[^s ]([JFMASOND][anuryebchpilgstmov]{2,8})[^s ]([0-9]{4}>)"
        .Replacement.Text = "\1^s\2^s\3"
        .Execute Replace:=wdReplaceAll
        'Ensure spaces before numbers are non-breaking
        .Text = " ([0-9])"
        .Replacement.Text = "^s\1"
        .Execute Replace:=wdReplaceAll
        'Ensure spaces after numbers are non-breaking
        .Text = "([0-9]) "
        .Replacement.Text = "\1^s"
        .Execute Replace:=wdReplaceAll
        'Ensure spaces before numbers in the array are ordinary spaces
        For i = 0 To UBound(ArrFnd)
          .Text = "^s([0-9]{1,}^s" & ArrFnd(i) & ")"
          .Replacement.Text = " \1"
          .Execute Replace:=wdReplaceAll
        Next
       .MatchWildcards = False
        'Delete white spaces before paragraph breaks
        .Text = "^w^p"
        .Replacement.Text = "^p"
        .Execute Replace:=wdReplaceAll
        'Delete white spaces after paragraph breaks
        .Text = "^p^w"
        .Execute Replace:=wdReplaceAll
        'Replace smart single quotes with straight single quotes
        .Text = "'"
        .Replacement.Text = Chr(39)
        .Execute Replace:=wdReplaceAll
        'Replace smart double quotes with straight double quotes
        .Text = """"
        .Replacement.Text = Chr(34)
        .Execute Replace:=wdReplaceAll
        'Delete periods in a.m./p.m.
        .MatchWildcards = True
        .Text = "[^s ]([ap]).m."
        .Replacement.Text = "^s\1m"
        .Execute Replace:=wdReplaceAll
        .Text = "[^s ]([ap]).m>"
        .Execute Replace:=wdReplaceAll
        'Delete spaces in # am/pm
        .Text = "([0-9])[^s ]([ap]m)"
        .Replacement.Text = "\1\2"
        .Execute Replace:=wdReplaceAll
        'Delete - following a : or ;
        .Text = "([\:\;\.])-"
        .Replacement.Text = "\1"
        .Execute Replace:=wdReplaceAll
        'Replace all double + spaces with single spaces of the same kind as the first
        .Text = "([^s ])[^s ]{1,}"
        .Execute Replace:=wdReplaceAll
        'Replace repeated . with single .
        .Text = "[.]{2,}"
        .Replacement.Text = "."
        .Execute Replace:=wdReplaceAll
        'Temporarily replace i.e. formatting
        .Text = "<i.e."
        .Replacement.Text = "ie"
        .Execute Replace:=wdReplaceAll
        .Text = "<ie>"
        .Execute Replace:=wdReplaceAll
        'Temporarily replace e.g. formatting
        .Text = "<e.g."
        .Replacement.Text = "eg"
        .Execute Replace:=wdReplaceAll
        .Text = "<eg>"
        'Temporarily replace etc. formatting
        .Execute Replace:=wdReplaceAll
        .Text = "<etc."
        .Replacement.Text = "etc"
        .Execute Replace:=wdReplaceAll
        .Text = "<etc>"
        .Execute Replace:=wdReplaceAll
        'Ensure H.M. & HM appear as HM followed by both a non-breaking space and an ordinary space
        .Execute Replace:=wdReplaceAll
        .Text = "<HM[^s ]{1,}"
        .Replacement.Text = "HM^s"
        .Execute Replace:=wdReplaceAll
        .Text = "<H.M.[^s ]{1,}"
        .Execute Replace:=wdReplaceAll
        'Ensure there are two ordinary spaces following . and ?
        .Text = "([.\?])[^s ]"
        .Replacement.Text = "\1  "
        .Execute Replace:=wdReplaceAll
        'Restore i.e., e.g. & etc. formatting
        .Text = ""
        .Replacement.Text = "."
        .Execute Replace:=wdReplaceAll
        'Remove hyphens from e-mail
        .Text = "e-mail"
        .Replacement.Text = "email"
        .Execute Replace:=wdReplaceAll
        'Delete non-breaking spaces before double-quotes
        .Text = "^s"""
        .Replacement.Text = """"
        .Execute Replace:=wdReplaceAll
        'Delete spaces before , : ; )
        .Text = "[^s ]([\,\:\;\.)])"
        .Replacement.Text = "\1"
        .Execute Replace:=wdReplaceAll
        'Replace non-breaking spaces with ordinary spaces before 'and' when followed by a non-breaking space
        .Text = "^sand^s"
        .Replacement.Text = " and^s"
        .Execute Replace:=wdReplaceAll
        'Ensure 'no.' is followed by a non breaking space
        .Text = "no.  "
        .Replacement.Text = "no.^s"
        .Execute Replace:=wdReplaceAll
        'Ensure 'etc...' only has one .
        .Text = "etc..."
        .Replacement.Text = "etc."
        .Execute Replace:=wdReplaceAll
         'Ensure 'etc..' only has one .
        .Text = "etc.."
        .Replacement.Text = "etc."
        .Execute Replace:=wdReplaceAll
      End With
    End With
    End Sub

  2. #2
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    As the comments preceding that line ('Ensure spaces before numbers in the array are ordinary spaces) indicate, the code
    Code:
     For i = 0 To UBound(ArrFnd)
          .Text = "^s([0-9]{1,}^s" & ArrFnd(i) & ")"
          .Replacement.Text = " \1"
          .Execute Replace:=wdReplaceAll
    only replaces the preceding non-breaking space with an ordinary space when it's followed by one of the ArrFnd items.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

Posting Permissions

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