Consulting

Page 2 of 2 FirstFirst 1 2
Results 21 to 25 of 25

Thread: Is it possible to loop a find and replace array?

  1. #21
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    You can't delete the last paragraph mark in a document. It looks like you have one empty paragraph in the document (the first ¶, and the document's final ¶)

    You can use a macro I guess to see if a paragraph is empty and delete it if it is a real issue

    Playing around -- I found it easier to just use subs instead of the array


    Option Explicit
    Sub ArrFndRep()
        
        '2 or more spaces to one space
        Call pvtWildcardFR(" {2,}", " ")
        
        '2 or more tabs to one tab
        Call pvtWildcardFR("^t{2,}", "^t")
        
        'space or tab + para to one para
        Call pvtWildcardFR("[ ,^t]{1,}^13", "^13")
        
        '2 or more para to one para
        Call pvtWildcardFR("^13{2,}", "^13")
        
    End Sub
    
    Private Sub pvtWildcardFR(Orig As String, Repl As String)
        With ActiveDocument.Range.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Forward = True
            .Format = False
            .MatchWholeWord = True
            .MatchCase = False
            .MatchWildcards = True
            .Wrap = wdFindContinue
            .Text = Orig
            .Replacement.Text = Repl
            .Execute Replace:=wdReplaceAll
        End With
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    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

  2. #22
    VBAX Tutor
    Joined
    Jul 2016
    Posts
    266
    Location
    Very interesting approach. Thanks Paul

  3. #23
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    If all you're worried about is that a final pair of paragraph breaks remains in your document after running the macro, that's not actually something Find/Replace can do anything about - because the final paragraph break cannot be processed. You'll find the same thing with the final paragraph before a table. They will be processed anywhere else in the document, however.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  4. #24
    VBAX Tutor
    Joined
    Jul 2016
    Posts
    266
    Location
    Thanks Paul. No I'm definitely not worried about having the paragraph breaks remaining. I just thought it was interesting that the final break has different properties. Thanks again guy for the help.

  5. #25
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Quote Originally Posted by Kilroy View Post
    Thanks Paul. No I'm definitely not worried about having the paragraph breaks remaining. I just thought it was interesting that the final break has different properties. Thanks again guy for the help.

    Word stores a lot of information about the Paragraph at the paragraph mark position in the in the document file

    IIRC, the last paragraph mark also includes Section information which is why it's 'special' and not deletable
    ---------------------------------------------------------------------------------------------------------------------

    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

Posting Permissions

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