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

    Help with macro to delete extra paragraph returns

    I have developed a macro that deletes extra return spaces in documents which works fine when the template has been formatted correctly. We use another case management system called Visual Files and these automated templates have not been formatted correctly with correct para/line spacing. This macro won't work in this CMS and deletes every return within the documents. I need this macro to only delete the returns when text is highlighted. Is this at all possible?

    Code:
    Sub DPU_2para1para()
        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        With Selection.Find
            .Text = "^p^p"
            .Replacement.Text = "^p"
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute Replace:=wdReplaceAll
    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
    You macro will not delete every return (paragraph break) in the document - it deletes only the second of consecutive returns. You could process all repeated returns more efficiently with code like:
    Code:
    Sub DPU_2para1para()
    With Selection.Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Text = "[^13]{2,}"
      .Replacement.Text = "^p"
      .Forward = True
      .MatchWildcards = True
      .Wrap = wdFindContinue
      .Execute Replace:=wdReplaceAll
    End With
    End Sub
    The above code will handle however many repeated returns your document might have. However, just as it isn't possible to highlight a return, you can't restrict a F/R to a highlighted set.

    On the other hand, if you mean selected returns instead of highlighted returns, change:
    wdFindContinue
    to:
    wdFindStop
    Last edited by macropod; 2016-06-16 at 08:13.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  3. The Following 3 Users Say Thank You to macropod For This Useful Post:

    Charles Kenyon (2016-06-16),Lady-Laughsalot (2016-06-16),RetiredGeek (2016-06-16)

  4. #3
    Star Lounger
    Join Date
    Aug 2015
    Posts
    98
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Sorry yes I meant selected and changing continue to stop has worked perfectly, thank you very much

  5. #4
    Star Lounger
    Join Date
    Aug 2015
    Posts
    98
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Hi, thanks for updating the macro, would it be possible to add changing line spacing from single to 1.5 to the same macro when text is selected after it has run the part to delete the extra spaces and text to be justified or would I need to run two macros?

  6. #5
    Silver Lounger Charles Kenyon's Avatar
    Join Date
    Jan 2001
    Location
    Sun Prairie, Wisconsin, Wisconsin, USA
    Posts
    2,049
    Thanks
    124
    Thanked 119 Times in 116 Posts
    You can have one macro do it. However, you would likely be ahead of the game applying styles rather than using direct formatting.
    Charles Kyle Kenyon
    Madison, Wisconsin

  7. #6
    Star Lounger
    Join Date
    Aug 2015
    Posts
    98
    Thanks
    14
    Thanked 0 Times in 0 Posts
    The documents are already formatted and automated through a case management system so I cannot physically change the templates and applying styles each time would be very time consuming so I wanted to design macro to delete the extra return spaces which the macro in #2 does and I wanted to add 1.5 line spacing and justification to it as currently each template is incorrectly set up. Thanks

  8. #7
    Star Lounger
    Join Date
    Aug 2015
    Posts
    98
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Hi, is anyone able to help me with #4 above please - many thanks

  9. #8
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    You could try adding the following line to the code:
    .Replacement.ParagraphFormat.LineSpacingRule = wdLineSpace1pt5
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  10. #9
    5 Star Lounger Lugh's Avatar
    Join Date
    Jun 2010
    Location
    Indy
    Posts
    627
    Thanks
    168
    Thanked 77 Times in 68 Posts
    Quote Originally Posted by Lady-Laughsalot View Post
    would I need to run two macros?
    A general FYI on that. You can always combine 2 or many macros into one. It looks like this:
    Code:
    Sub MainMacro()
    Call Macro1
    Call Macro2
    Call Macro3
    Call Macro4
    End Sub
    "Macro1" etc are the names of other individual macros, eg Sub Macro1().

    I find it useful to break large jobs into manageable chunks with this approach, so that dozens of tiny macros are combined into say 4-6 main macros. These 4-6 can then be put on the Word ribbon as buttons with labels, which makes it simple to use.
    Lugh.
    ~
    Windows 10 Pro x64 1607; Office 2016 (365 Home) x32; Win Defender, MBAM Pro

    ASRock H97 Anniversary; Xeon E3-1231V3 (like i7)
    Gigabyte GeForce GTX 970; 12GB Crucial DDR3 1600
    Logitech MX Master mouse; Roccat Isku kb

  11. The Following User Says Thank You to Lugh For This Useful Post:

    Charles Kenyon (2016-06-22)

  12. #10
    Star Lounger
    Join Date
    Aug 2015
    Posts
    98
    Thanks
    14
    Thanked 0 Times in 0 Posts
    I have copied your suggestion in #8 but it comes up with a compile error invalid or unqualified reference and highlights .Replacement.

    The idea for this macro is not only to delete the extra return spaces but to change the line spacing from single to 1.5 lines with paragraph spacing of 0pt before and 12pt after which is compliant with British Court Rules for setting out court documents.

    Code:
    Sub DPU_DeleteSpacesVF()
    With Selection.Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Text = "[^13]{2,}"
      .Replacement.Text = "^p"
      .Forward = True
      .MatchWildcards = True
      .Wrap = wdFindStop
      .Execute Replace:=wdReplaceAll
    End With
    .Replacement.ParagraphFormat.LineSpacingRule = wdLineSpace1pt5
    End Sub

  13. #11
    Star Lounger
    Join Date
    Aug 2015
    Posts
    98
    Thanks
    14
    Thanked 0 Times in 0 Posts
    I have been trying to update the macro and have come up with this - it seems to work but wondered if it could be shorter.

    Code:
    Sub DPU_DeleteSpacesVF()
    With Selection.Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Text = "[^13]{2,}"
      .Replacement.Text = "^p"
      .Forward = True
      .MatchWildcards = True
      .Wrap = wdFindStop
      .Execute Replace:=wdReplaceAll
    End With
     With Selection.ParagraphFormat
            .LeftIndent = InchesToPoints(0.5)
            .RightIndent = InchesToPoints(0)
            .SpaceBefore = 0
            .SpaceBeforeAuto = False
            .SpaceAfter = 12
            .SpaceAfterAuto = False
            .LineSpacingRule = wdLineSpace1pt5
            .Alignment = wdAlignParagraphJustify
            .WidowControl = True
            .KeepWithNext = False
            .KeepTogether = False
            .PageBreakBefore = False
            .NoLineNumber = False
            .Hyphenation = True
            .FirstLineIndent = InchesToPoints(-0.5)
            .OutlineLevel = wdOutlineLevel1
            .CharacterUnitLeftIndent = 0
            .CharacterUnitRightIndent = 0
            .CharacterUnitFirstLineIndent = 0
            .LineUnitBefore = 0
            .LineUnitAfter = 0
            .MirrorIndents = False
            .TextboxTightWrap = wdTightNone
         End With
        Selection.ParagraphFormat.Alignment = wdAlignParagraphJustify
    End Sub

  14. #12
    5 Star Lounger Lugh's Avatar
    Join Date
    Jun 2010
    Location
    Indy
    Posts
    627
    Thanks
    168
    Thanked 77 Times in 68 Posts
    Quote Originally Posted by Lady-Laughsalot View Post
    it seems to work but wondered if it could be shorter.
    Charles said it earlier, don't macro the formatting, do it via styles. Sounds like you might only need to modify the default paragraph style.
    Quote Originally Posted by Charles Kenyon View Post
    you would likely be ahead of the game applying styles rather than using direct formatting.
    Lugh.
    ~
    Windows 10 Pro x64 1607; Office 2016 (365 Home) x32; Win Defender, MBAM Pro

    ASRock H97 Anniversary; Xeon E3-1231V3 (like i7)
    Gigabyte GeForce GTX 970; 12GB Crucial DDR3 1600
    Logitech MX Master mouse; Roccat Isku kb

  15. #13
    Star Lounger
    Join Date
    Aug 2015
    Posts
    98
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Hi thanks for the reply, unfortunately I can't do that as mentioned above as these templates are automated through a case management system and therefore the default paragraph style cannot be tampered with, hence wanting a macro to quickly perform this command quickly in word documents once automated. The above code seems to work but I thought it could be made simpler with better coding. Thanks anyway. Shelley

  16. #14
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Quote Originally Posted by Lady-Laughsalot View Post
    I have copied your suggestion in #8 but it comes up with a compile error invalid or unqualified reference and highlights .Replacement.
    Since it's a .Replacement expression to be incorporated into the Find/Replace, I'd have though it pretty obvious you'd use:
    Code:
    Sub DPU_DeleteSpacesVF()
    With Selection.Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Text = "[^13]{2,}"
      .Replacement.Text = "^p"
      .Replacement.ParagraphFormat.LineSpacingRule = wdLineSpace1pt5
      .Forward = True
      .MatchWildcards = True
      .Wrap = wdFindStop
      .Execute Replace:=wdReplaceAll
    End With
    End Sub
    with '.Replacement.ParagraphFormat.LineSpacingRule = wdLineSpace1pt5' being inserted anywhere between '.Replacement.ClearFormatting' and '.Execute Replace:=wdReplaceAll'.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

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

    Charles Kenyon (2016-06-22)

  18. #15
    Star Lounger
    Join Date
    Aug 2015
    Posts
    98
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Thanks Macropod much appreciated, I have added further code and it works great and saving a lot of time. By way of interest, I would like to set up a similar one for correspondence and need line spacing of at least 15, but I can't get the code to work properly.

    Code:
    .Replacement.ParagraphFormat.LineSpacingRule - wdLineSpaceAtLease15


    Code:
    Sub DPU_VFCourt()
    With Selection.Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Text = "[^13]{2,}"
      .Replacement.Text = "^p"
      .Replacement.ParagraphFormat.SpaceBefore = 0
      .Replacement.ParagraphFormat.SpaceAfter = 12
      .Replacement.ParagraphFormat.LineSpacingRule = wdLineSpace1pt5
      .Replacement.ParagraphFormat.Alignment = wdAlignParagraphJustify
      .Forward = True
      .MatchWildcards = True
      .Wrap = wdFindStop
      .Execute Replace:=wdReplaceAll
    End With
    End Sub

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
  •