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

    Inserting hardspace before cross reference fields in Word 2010

    I work with cross references on a daily basis and our company house style is that a hardspace is to be inserted wherever there is a digit in a document. I cannot find anywhere on the internet/forums on how to insert a hardspace before a cross reference field. I would like to create a macro that instances changes spaces to hardspaces wherever there is a digit - the only way I've managed to do this so far is to find and replace e.g. clause, paragraph, parts, schedules, appendices etc. and insert a hardspace after these words. The problem is this works to a certain extent except when referring to sections in their plural e.g. clauses 2.1 and 2.2 or clauses 2.1 to 2.5 - my macro does not pick up cross ref fields before 'and' and 'to'. Any help on this would be greatly appreciated.

  2. #2
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts
    Try this macro
    Code:
    Sub HardSpaceBeforeCrossRefs()
      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
    End Sub
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  3. The Following User Says Thank You to Andrew Lockton For This Useful Post:

    Lady-Laughsalot (2015-08-07)

  4. #3
    Star Lounger
    Join Date
    Aug 2015
    Posts
    98
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Thank you so much for this Andrew, this macro works a treat and is already saving me a heap of time amending documents. Fabulous

    Hi Andrew, I am trying to build on your macro and change space either side of a non-fielded digit and replace with a hardspace but it doesn't seem to be working. Currently I have:

    Selection.Find.Execute Replace:=wdReplaceAll
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
    .Text = "( )[0-9]"
    .Replacement.Text = "^s^&"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchAllWordForms = False
    .MatchSoundsLike = False
    .MatchWildcards = True
    End With

    Not sure where I'm going wrong - can you help me at all please.
    Thanks
    Shelley
    Last edited by Lady-Laughsalot; 2015-08-07 at 10:14.

  5. #4
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts
    Try
    Code:
    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
    Andrew Lockton, Chrysalis Design, Melbourne Australia

Posting Permissions

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