none
Replace Paragraph Marks Between 2 Words RRS feed

  • Question

  • Hi friends,

    I am trying to search and replace some paragraph marks between 2 place holders text


    Example all the paragraph marks between CP100 and CPX replace with a #

    I have got started but am now stuck



    Sub ReplaceParaMarks()
    
    
      Dim oRng As Range
        Dim oFind As Range
        Set oRng = ActiveDocument.Range
        Set oFind = ActiveDocument.Range
        'With oFind.Find
    
          With oRng.Find
          Do While .Execute(FindText:="[A-Z]{2,}[0-9]{3,}", MatchWildcards:=True)
               
           Do Until oRng.Next.Words(1) = "CPX"
           
           With Selection.Find
            .Text = "^P"
            .Replacement.Text = "#"
            .Forward = True
            .Wrap = wdFindContinue
            
            End With
            End With
            
           
         End Sub
           



    am i on the right track and how can i fix this

    Cheers Dan :)

    Tuesday, September 6, 2016 12:29 AM

Answers

  • How about


    Sub ReplaceParaMarks()
    Dim oRng As Range
        Set oRng = ActiveDocument.Range
        With oRng.Find
            Do While .Execute(FindText:="CP100*CPX", MatchWildcards:=True)
                oRng.Text = Replace(oRng.Text, Chr(13), " ")
                oRng.Collapse 0
            Loop
        End With
    End Sub



    Graham Mayor - Word MVP
    www.gmayor.com

    • Marked as answer by Dan_CS Tuesday, September 6, 2016 12:24 PM
    Tuesday, September 6, 2016 4:16 AM

All replies

  • How about


    Sub ReplaceParaMarks()
    Dim oRng As Range
        Set oRng = ActiveDocument.Range
        With oRng.Find
            Do While .Execute(FindText:="CP100*CPX", MatchWildcards:=True)
                oRng.Text = Replace(oRng.Text, Chr(13), " ")
                oRng.Collapse 0
            Loop
        End With
    End Sub



    Graham Mayor - Word MVP
    www.gmayor.com

    • Marked as answer by Dan_CS Tuesday, September 6, 2016 12:24 PM
    Tuesday, September 6, 2016 4:16 AM
  • Graham,

    thank you so much it did the trick and very nicely too :)

    And in less code!

    This is a great new code snippet for me - I have not come accross the * star in between

    Do While .Execute(FindText:="CP100*CPX"

    It took me a couple of hours of messing up my code to forget where i was in the begining

    Have a great day :)


    Cheers Dan :)

    Tuesday, September 6, 2016 12:24 PM
  • Tuesday, September 6, 2016 1:52 PM