#1
|
|||
|
|||
Replace space with paragraph mark
The code below works find to achieve the results below; however, it does not change just the selection. It changes other areas of the document which I don't want touched.
Each line in the before is a line with a paragraph mark at the end of each line. Before A2.3. Walk the dog A2.3.4.5. Feed the dog A2.3.4. Bath the dog A2.3. Walk the Cat A2.3.4.5. Feed the Cat After A2.3. Walk the dog A2.3.4.5. Feed the dog A2.3.4. Bath the dog A2.3. Walk the Cat A2.3.4.5. Feed the Cat After all the paragraph marks are inserted, each line with begin with A2. if that makes a difference. Code:
Sub FixReferences1() Dim oRng As Range Set oRng = ActiveDocument.Range With Selection With oRng.Find .ClearFormatting .Replacement.ClearFormatting .Replacement.Font.Bold = False .Text = "[ ^s^t](<[0-9A-Z]{2,})" .Replacement.Text = "^p\1" .Forward = True .Wrap = wdFindContinue .Format = True .MatchCase = False .MatchWholeWord = False .MatchAllWordForms = False .MatchSoundsLike = False .MatchWildcards = True .Execute Replace:=wdReplaceAll End With End With End Sub |
#2
|
||||
|
||||
If each line starts with A2 then
Code:
Sub FixReferences1() Dim oRng As Range Set oRng = ActiveDocument.Range With oRng.Find .ClearFormatting .Replacement.ClearFormatting .Replacement.Font.Bold = False .Text = " A2" .Replacement.Text = "^pA2" .Forward = True .Wrap = wdFindContinue .Format = True .MatchCase = False .MatchWholeWord = False .MatchAllWordForms = False .MatchSoundsLike = False .MatchWildcards = False .Execute Replace:=wdReplaceAll End With End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
||||
|
||||
Change:
.Wrap = wdFindContinue to: .Wrap = wdFindStop
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#4
|
||||
|
||||
And change:
.Text = " A2" to: .Text = "^wA2" if you wanted to include any type of white space in front of A2
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#5
|
|||
|
|||
I've tried changing to .Wrap = wdFindStop, but it still is acting on other parts of the document besides what is highlighted.
Here is the documents I'm testing on. Andrew and Graham, thank you, the " A2" method works well. |
#6
|
||||
|
||||
Actually, the basic problem with your code is that you're explicitly telling the Find to act on the whole document, not just the selected range. Graham's code does likewise, but the .Text = " A2" Find expression works only because your selection is the only range that contains that string. Try:
Code:
Sub FixReferences() With Selection.Find .ClearFormatting .Replacement.ClearFormatting .Text = " A2" .Replacement.Text = "^pA2" .Forward = True .Format = False .Wrap = wdFindStop .MatchWildcards = True .Execute Replace:=wdReplaceAll End With End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
Thank you Paul works great and thanks for the explanation.
Can you explain? .Text = "[ ^s^t](<[0-9A-Z]{2,})" I understand the <[0-9A-Z]{2,} is looking for any letter number combination and at least 2 characters from the beginning of the word, but what is the first part? |
#8
|
||||
|
||||
The [ ^s^t] represents any of a space, non-breaking space, or a tab.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#9
|
|||
|
|||
Awesome, thanks again. Your help along with the others has been invaluable.
|
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
can't delete paragraph mark at end of document | kb | Word | 10 | 10-06-2017 02:32 PM |
Suppress paragraph mark in mail merge field | acerview | Mail Merge | 3 | 12-22-2015 07:44 PM |
Paragraph mark after horizontal line is way to the right. | DBlomgren | Word | 0 | 02-09-2014 11:42 PM |
Final paragraph mark | Caroline | Word | 2 | 02-22-2011 10:39 AM |
Adding a paragraph mark by style? | Jazz43 | Word | 0 | 02-14-2011 06:08 AM |