Results 1 to 8 of 8

Thread: Help - macro to insert a return before each capital letter in selected text failing

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #5
    VBAX Regular
    Joined
    Jan 2021
    Posts
    13
    Location
    Hi,

    Thanks for your suggestion. I very much appreciate it. Unfortunately it chrashes on "Selection.Find.Execute Replace:=wdReplaceAll" with the error message, Run-time error ‘5623’, “The replacement text contains a group number that’s out of range

    Anyway, I do not think your suggestion will do what I want. Your replacement statement in 'Selection.Find' looks like it will replace the capital letter with a line break. I do not want the capital letter replaced. I want a line break (return whatever) inserted before the capital letters. I need the capital letters since they are an integral part of the text.

    Since my original post I have revised my macro to the following:

    With Selection.Find
        Selection.Find.ClearFormatting
        With Selection.Find
            .Text = "[A-Z]"
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = True
            .MatchSoundsLike = False
            .MatchAllWordForms = False
    End With
    Do While Selection.Find.Found = True
    Selection.InsertBefore vbNewLine
    Loop
    End With
    This is the text I am working with:

    All that have young people, good manners set them to learn; To their elders with gentle conditions let do nor say no harm. If they do ill, wise men may report their parents soon How should they teach other good, belike themselves cary none. A good father maketh good children, if wisdom be them within,

    When I run this macro (with a stop statement) I get the following result:




    All that have young people, good manners set them to learn; To their elders with gentle conditions let do nor say no harm. If they do ill, wise men may report their parents soon How should they teach other good, belike themselves cary none. A good father maketh good children, if wisdom be them within,

    It would seem that the find statement is not advancing through the selected text. Instead, it keeps finding the very first capital letter and only inserting a line break before this capital letter (note: to get the above result I ran the macro three times with a Stop statement). I have tried starting it at the second capital letter, but I get the same result.

    I have tried changing the line break insertion Do While loop, but to no avail e.g.:

    Do while Selection.Find.Found = true or Selection.Find.Execute

    So if you or anyone else has any suggestions as to how I can make the Selection.Find statement run through the whole selected text, I would be grateful.

    My problem seems to be that I cannot get the Selection.Find statement to advance to each capital letter. This is even though I have Wrap set to 'Wrap = wdFindContinue". I am not sure where the problem lies: in the Selection.Find statement or in the Do Loop/Execute statement.

    Thanks again for your suggestion. I very much appreciate it.
    Last edited by Aussiebear; 11-16-2022 at 01:31 PM. Reason: Added code tags to supplied code

Posting Permissions

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