View Single Post
 
Old 04-29-2023, 04:38 AM
ctviggen ctviggen is offline Windows 10 Office 2016
Advanced Beginner
 
Join Date: Feb 2021
Posts: 54
ctviggen is on a distinguished road
Default

Thank you very much. That is a great resource. I had no idea search and replace was so powerful.

This is what I ended up doing. I tried to add a space as a wildcard (before a paragraph return), but that did not work. Since I didn't have time to figure out why, I just decided to remove spaces before a paragraph return, removing up to three spaces. This might not always work, but I plan to have a document people can read so they know what each macro does, where I make this a caveat. At some time in the future, maybe I can analyze and correct so that no matter how many spaces you have, it'll work.

And this is highly commented for a few reasons. One (as evidenced by the time it takes me to get back to threads like this) is because I take a long time between being able to code, and I want to remember how things work. Two, I hope to have someone else take over. Three, someone gave me code to use. I THINK I know what it does, but it's completely without comments and line after line of code. Sure, I can step through it, but you can't take time to put in any comments?



Here's the code:


Code:
    ' Attempt to make every example as one paragraph
    ' This part removes spaces from before a paragraph, up to three spaces
    With Selection.Find
        .Text = " ^p"
        .Replacement.Text = "^p"
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    Selection.Find.Execute Replace:=wdReplaceAll
    Selection.Find.Execute Replace:=wdReplaceAll
    
    ' This finds the paragraphs without a period and replaces the paragraph character with a space
    '   This needs to have MatchWildCards = true
    '   because there are wildcards in the Find.Text part
    '   For wildcards, see https://wordmvp.com/FAQs/General/UsingWildcards.htm
    With Selection.Find
        ' This seems like it should work: adds a space as a wildcard, but this does not work
        ' .Text = "([:;,a-z ])^13"
        .Text = "([:;,a-z])^13"
        .Replacement.Text = "\1 "
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = True
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
Reply With Quote