Consulting

Results 1 to 6 of 6

Thread: Include end paragraph mark of previous paragraph in selection

  1. #1
    VBAX Contributor
    Joined
    Mar 2007
    Posts
    140
    Location

    Include end paragraph mark of previous paragraph in selection

    Hello All,

    Some time ago Greg Maxey helped me out with a find and replace macro that operates within a selected range.
    I wanted to insert a tab space after a right parenthesis and found a Cybertext article that uses wildcards to find virtually what I was after:
    https://cybertext.wordpress.com/2015...g-punctuation/

    Using most of Greg’s macro I have modified this to search for a number followed by a right parentheses using a wildcard search based on the Cybertext article.

    If for example, using the ‘quick brown fox’ text below, I select the first three paragraphs it works fine. However, if I select just the paragraphs with the numbers and right parenthesis it does not inset a tab after the number 1 as the end paragraph mark in the first paragraph was not included in the initial selected range.

    The quick brown fox jumps over the lazy dog.
    1) The quick brown fox jumps over the lazy dog.
    2) The quick brown fox jumps over the lazy dog.
    The quick brown fox jumps over the lazy dog.

    What changes do I need to make to the following macro to include the end paragraph mark of the previous paragraph that was not included in the initial selection ???

    Sub FindRightParenthesisWithinSelection()
    'Code written by Greg Maxey
    
    
      Dim myRange As Range
      Dim oRngBounds As Range
      
      Set myRange = Selection.Range
      Set oRngBounds = myRange.Duplicate
      
      With myRange.Find
        'Find end paragraph mark then number then right ) followed by a space
        .Text = "(^013)([0-9]@)([\)])([ ])"
        .wrap = wdFindStop
        .MatchWildcards = True
        .Forward = True
        Do While .Execute
          If Not myRange.InRange(oRngBounds) Then Exit Sub
          myRange.Select
          ActiveWindow.ScrollIntoView Selection.Range
          Select Case MsgBox("Do you want to insert a tab after this bracket?", vbQuestion + vbYesNoCancel, "Insert tab after bracket")
          Case vbYes
             myRange.Characters.Last = vbTab
          Case vbNo
            'Do nothing.
          Case Else
            GoTo lbl_Exit
          End Select
          myRange.Collapse wdCollapseEnd
        Loop
      End With
    lbl_Exit:
      Exit Sub
    End Sub
    Regards,
    Dave T

  2. #2
    VBAX Regular
    Joined
    Jul 2014
    Posts
    79
    Location
    Could you not just change .Text = "(^013)([0-9]@)([\)])([ ])" to .Text = "([0-9]@)([\)])([ ])"







  3. #3
    VBAX Contributor
    Joined
    Mar 2007
    Posts
    140
    Location
    Hello Jfp87,

    I appreciate your reply.

    Yes I did try that, but sometimes the paragraph may contain more than one number followed by a right parentheses and this option affects each case.
    For example:

    The quick brown fox jumps over the lazy dog.
    1) The quick brown fox jumps 2) the low brown fox flops.
    3) The quick brown fox jumps over the lazy dog 4) the lazy dog.
    The quick brown fox jumps over the lazy dog.

    I only want a tab inserted after the first parentheses at the start of each paragraph and not each case after that, hence finding the end paragraph mark of the previous paragraph.

    I also had another question as to whether a specific sized hanging indent and tab stop could be incorporated into the macro that was independent of the left indent setting.
    For example the left indent could be 1.0cm or 1.5cm or whatever, but the hanging and tab would always be say 1.0cm from whatever the size the left indent may be set to.

    Regards,
    Dave T

  4. #4
    VBAX Regular
    Joined
    Jul 2014
    Posts
    79
    Location
    Try this:

    Sub FindRightParenthesisWithinSelection()'Code written by Greg Maxey
    Dim myRange As Range
    Dim oRngBounds As Range
    
    
      Set myRange = Selection.Range
      Set oRngBounds = myRange.Duplicate
      
      With myRange.Find
        'Find end paragraph mark then number then right ) followed by a space
        .Text = "([0-9]@)([\)])([ ])"
        .Wrap = wdFindStop
        .MatchWildcards = True
        .Forward = True
        
        Do While .Execute
        
          If Not myRange.InRange(oRngBounds) Then Exit Sub
          
          If myRange.Start = myRange.Paragraphs(1).Range.Start Then
          
            myRange.Select
            ActiveWindow.ScrollIntoView Selection.Range
            
            Select Case MsgBox("Do you want to insert a tab after this bracket?", vbQuestion + vbYesNoCancel, "Insert tab after bracket")
            Case vbYes
               myRange.Characters.Last = vbTab
            Case vbNo
              'Do nothing.
            Case Else
              GoTo lbl_Exit
            End Select
            
            myRange.Collapse wdCollapseEnd
            
          End If
          
        Loop
        
      End With
      
    lbl_Exit:
      Exit Sub
    End Sub

  5. #5
    VBAX Contributor
    Joined
    Mar 2007
    Posts
    140
    Location
    Hello Jfp86,

    You solution works perfectly. Thank you for your help.
    I had worked out that my selected range needed to go back a couple of characters, but I could not work out how to do it.

    Very nice, quick solution.

    Regards
    Dave T

  6. #6
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by Dave T View Post
    sometimes the paragraph may contain more than one number followed by a right parentheses and this option affects each case.
    For example:

    The quick brown fox jumps over the lazy dog.
    1) The quick brown fox jumps 2) the low brown fox flops.
    3) The quick brown fox jumps over the lazy dog 4) the lazy dog.
    The quick brown fox jumps over the lazy dog.

    I only want a tab inserted after the first parentheses at the start of each paragraph and not each case after that, hence finding the end paragraph mark of the previous paragraph.
    In that case, all you need is a wildcard Find/Replace with:
    Find = ([0-9]{1,}\)) (*^13)
    Replace = \1^t\2
    and use 'Replace' instead of 'Replace All' to have Word ask whether the insert the tab in a specific instance. No macros required.
    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
  •