Consulting

Results 1 to 10 of 10

Thread: Remove soft and/or hard returns

  1. #1

    Remove soft and/or hard returns

    Hi,

    I'm pretty lousy at Word vba (although I can still write stuff in nexcel despite having been retuired 12 years).
    Is it possible for anyone to tell me how I can use vba to go through a document and remove a return (either soft or hard) if there are two or more consecutively? I am archiving content from a forum which is about to close and the software is very good at putting extra returns in - and the document is currently at about 500 pages.

    Thanks in advance
    Paul

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    Sub ScratchMacro()
    'A basic Word macro coded by Greg Maxey
    Dim oRng As Range
      Set oRng = ActiveDocument.Range
      With oRng.Find
        .Text = Chr(13) & "{2,}"
        .Replacement.Text = Chr(13)
        .MatchWildcards = True
        .Execute Replace:=wdReplaceAll
      End With
      Set oRng = ActiveDocument.Range
      With oRng.Find
        .Text = Chr(11) & "{2,}"
        .Replacement.Text = Chr(11)
        .MatchWildcards = True
        .Execute Replace:=wdReplaceAll
      End With
    lbl_Exit:
      Exit Sub
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    Many thanks Greg, that has done the trick. It has cleared about 50 pages (of c500)!

    Paul

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Simpler and faster:
    Sub Demo()
    Application.ScreenUpdating = False
    With ActiveDocument.Range.Find
      .Text = "([^13^l])[^13^l]{1,}"
      .Replacement.Text = "\1"
      .MatchWildcards = True
      .Execute Replace:=wdReplaceAll
    End With
    Application.ScreenUpdating = True
    End Sub
    Last edited by macropod; 05-13-2020 at 05:47 PM.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,711
    Location
    Greg --

    What about Chr(13)Chr(11) and Chr(11)Chr(13) pairs?

    I'd think you'd want them down to just a ^p
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  6. #6
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    The way the Find/Replace expressions are coded, it doesn't matter which way the [^13^l] (or [^13^11]) sequence is written. Moreover, whichever character in a Chr(13) & Chr(11) sequence occurs first, that is the character that gets used for the replacement. It's only where someone has foolishly used manual line breaks to add padding below a paragraph that things might come unstuck.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  7. #7
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,711
    Location
    Quote Originally Posted by macropod View Post
    The way the Find/Replace expressions are coded, it doesn't matter which way the [^13^l] (or [^13^11]) sequence is written. Moreover, whichever character in a Chr(13) & Chr(11) sequence occurs first, that is the character that gets used for the replacement. It's only where someone has foolishly used manual line breaks to add padding below a paragraph that things might come unstuck.

    If I'm reading Greg's macro correctly, it replaced 2 or more CR's with a single CR, and 2 or more LF's with a single LF

    I was wondering about the case where there was a CR-LF pair or a LF-CR pair that the OP wanted turned into a single CR
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  8. #8
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by Paul_Hossler View Post
    If I'm reading Greg's macro correctly, it replaced 2 or more CR's with a single CR, and 2 or more LF's with a single LF
    His original macro, yes. What it didn't handle was a was a CR-LF pair or a LF-CR pair.
    Quote Originally Posted by Paul_Hossler View Post
    I was wondering about the case where there was a CR-LF pair or a LF-CR pair that the OP wanted turned into a single CR
    My macro and Greg's second handle all combinations as described in post #8.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  9. #9
    Thank you both for your help. I do have instances of a soft return followed by a hard return and vv, but from a quick glance, most if not all of those seem to have been catered for and I'm happy sorting out the others - it's a lot better than going through 500 pages and deleting them one at a time

  10. #10
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    In that case:
    Sub Demo()
    Application.ScreenUpdating = False
    With ActiveDocument.Range.Find
      .MatchWildcards = True
      .Text = "[^13]{2,}"
      .Replacement.Text = "^p"
      .Execute Replace:=wdReplaceAll
      .Text = "[^l]{2,}"
      .Replacement.Text = "^l"
      .Execute Replace:=wdReplaceAll
      .Text = "[^13^l]{2,}"
      .Replacement.Text = "^p"
      .Execute Replace:=wdReplaceAll
    End With
    Application.ScreenUpdating = True
    End Sub
    Cheers
    Paul Edstein
    [Fmr 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
  •