Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-21-2018, 05:41 PM
jeffreybrown jeffreybrown is offline Replace space with paragraph mark Windows Vista Replace space with paragraph mark Office 2007
Expert
Replace space with paragraph mark
 
Join Date: Apr 2016
Posts: 673
jeffreybrown has a spectacular aura aboutjeffreybrown has a spectacular aura about
Default 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
Reply With Quote
  #2  
Old 08-21-2018, 08:17 PM
gmayor's Avatar
gmayor gmayor is offline Replace space with paragraph mark Windows 10 Replace space with paragraph mark Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #3  
Old 08-21-2018, 08:30 PM
macropod's Avatar
macropod macropod is offline Replace space with paragraph mark Windows 7 64bit Replace space with paragraph mark Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Change:
.Wrap = wdFindContinue
to:
.Wrap = wdFindStop
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #4  
Old 08-21-2018, 09:18 PM
Guessed's Avatar
Guessed Guessed is offline Replace space with paragraph mark Windows 10 Replace space with paragraph mark Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #5  
Old 08-22-2018, 02:44 PM
jeffreybrown jeffreybrown is offline Replace space with paragraph mark Windows Vista Replace space with paragraph mark Office 2007
Expert
Replace space with paragraph mark
 
Join Date: Apr 2016
Posts: 673
jeffreybrown has a spectacular aura aboutjeffreybrown has a spectacular aura about
Default

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.
Attached Files
File Type: docm Testing2.docm (17.9 KB, 7 views)
Reply With Quote
  #6  
Old 08-22-2018, 03:12 PM
macropod's Avatar
macropod macropod is offline Replace space with paragraph mark Windows 7 64bit Replace space with paragraph mark Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #7  
Old 08-22-2018, 03:22 PM
jeffreybrown jeffreybrown is offline Replace space with paragraph mark Windows Vista Replace space with paragraph mark Office 2007
Expert
Replace space with paragraph mark
 
Join Date: Apr 2016
Posts: 673
jeffreybrown has a spectacular aura aboutjeffreybrown has a spectacular aura about
Default

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?
Reply With Quote
  #8  
Old 08-22-2018, 03:27 PM
macropod's Avatar
macropod macropod is offline Replace space with paragraph mark Windows 7 64bit Replace space with paragraph mark Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

The [ ^s^t] represents any of a space, non-breaking space, or a tab.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #9  
Old 08-22-2018, 03:31 PM
jeffreybrown jeffreybrown is offline Replace space with paragraph mark Windows Vista Replace space with paragraph mark Office 2007
Expert
Replace space with paragraph mark
 
Join Date: Apr 2016
Posts: 673
jeffreybrown has a spectacular aura aboutjeffreybrown has a spectacular aura about
Default

Awesome, thanks again. Your help along with the others has been invaluable.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Replace space with paragraph mark can't delete paragraph mark at end of document kb Word 10 10-06-2017 02:32 PM
Replace space with paragraph mark 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
Replace space with paragraph mark 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

Other Forums: Access Forums

All times are GMT -7. The time now is 07:25 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft