![]() |
#1
|
|||
|
|||
![]()
We have text like this:
Start: {paragraph return} line 1; {paragraph return} line 2; {paragraph return} ending line.{paragraph return} That is, everything but the ending line could have a colon, semicolon, comma..., but ONLY the "ending line" has a period. This is the only period. I want to end up with: Start: line 1; line 2; ending line.{paragraph return} I already have code that manipulates this for various reasons. I will add code that searches for "space ^p" (a space and then a paragraph return) and replaces with just "^p" (a paragraph return). I'll run this a few times, so that I should end up with "character ^p", each line ending with some type of character then an immediate paragraph return. What I would like to implement is the following: If the line does not end with a ".^p" but does end with a "^p", replace the "^p" with a space. This removes the paragraph return from every line but those that end with a period and paragraph return. I could "brute force" this by replacing all ";^p" with a space, all ",^p" with a space, ":^p" with a space. But is there a more elegant way of doing this? |
#2
|
||||
|
||||
![]()
Try a wildcard search
Find: ([:;,])^13 Replace: \1{space}
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#3
|
|||
|
|||
![]()
Thank you, and sorry for my delay in responding. I'm now limited to working on macros only at lunch, so it is taking me a while to do anything.
How do I implement what you're suggesting? I tried this, but it's incorrect: Code:
Selection.Find.Execute FindText:=([:;,]) ^ 13, ReplaceWith:="\1 ", Replace:=wdReplaceAll What does the ^13 do? Anyway, I was looking at this, and it's "worse" than I thought. I forgot that lines can also end with characters, e.g., a line could end with "d" from "and" or "r" from "or" or a number of other words. Ugh. So, basically, after processing, a period followed by a paragraph return should NOT be changed, but every other line ending in a paragraph return SHOULD be modified by replacing the paragraph return with a space. But your example could get me partway there. |
#4
|
||||
|
||||
![]()
When using a wildcard search you can't find a paragraph mark with a ^p but you can if you search for ^13 which is its ASCII value.
You can include paragraphs ending with a letter by including a-z inside the square brackets. Have a look at this page for basic info on wildcard searches. Finding and replacing characters using wildcards
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#5
|
|||
|
|||
![]()
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 |
#6
|
||||
|
||||
![]()
You can find whitespace (^w) rather than just a single space to do the first replaces in one pass.
Your code was using selection and stopping at the end of the document (.Wrap) and it never moved the selection back to the top for the second pass so the second part could only search down after the last found item from the first replacements. f you open a With Selection.Find, it doesn't make sense to do an End With and immediately follow it with more Selection.Find commands. I know Microsoft records a macro like this but it is one of my pet hates. Try this modified code Code:
Sub ReplaceMe() ' Attempt to make every example as one paragraph ' This part removes spaces from before a paragraph, up to three spaces With ActiveDocument.Range.Find .ClearFormatting 'ensures no sticky formatting remains from earlier searches .Text = "^w^p" 'finds whitespace (tab or space) followed by a paragraph mark .Replacement.Text = "^p" 'replace with just the paragraph mark .Forward = True .Wrap = wdFindContinue 'wraps back to continue from top if search didn't start there .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False .Execute Replace:=wdReplaceAll ' Finds paragraphs ending with lowercase letter, comma, colon or semicolon 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 .Text = "([:;,a-z])^13" .Replacement.Text = "\1 " .MatchWildcards = True .Execute Replace:=wdReplaceAll End With End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Split apart "page break" and "paragraph mark" | EwenMc | Word | 4 | 10-03-2021 02:12 AM |
![]() |
Dart82 | Word VBA | 3 | 10-04-2013 01:47 PM |
Wierd symbols inplace of "space", "indentation" etc | aka.bhagvanji | Word | 5 | 02-16-2012 11:50 AM |
![]() |
Jamal NUMAN | Word | 1 | 07-06-2011 04:25 AM |
![]() |
MShroff | Word | 8 | 01-19-2011 08:43 AM |