Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-24-2017, 02:22 PM
eduzs eduzs is offline Replacing till there's no more double paragraphs in the text Windows 10 Replacing till there's no more double paragraphs in the text Office 2010 32bit
Expert
Replacing till there's no more double paragraphs in the text
 
Join Date: May 2017
Posts: 262
eduzs is on a distinguished road
Default Replacing till there's no more double paragraphs in the text

Search for "^p^p" and replace with "^p" loop this until it no find any one more "^p^p" in the active document.

I have this code:

(it's not working, its resulting in freeze/loop/crash word)

Dim oRng As Range

Set oRng = ActiveDocument.Range

With oRng.Find

.Wrap = wdFindStop

Do While .Execute(FindText:="^p^p",replacewith:="^p")



Loop

End With
__________________
Backup your original file before doing any modification.
Reply With Quote
  #2  
Old 06-24-2017, 05:26 PM
gmaxey gmaxey is offline Replacing till there's no more double paragraphs in the text Windows 7 32bit Replacing till there's no more double paragraphs in the text Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Code:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRng As Range
  Set oRng = ActiveDocument.Range
  With oRng.Find
      .Text = "^13{2,}"
      .Replacement.Text = "^p"
      .MatchWildcards = True
      .Execute Replace:=wdReplaceAll
    End With
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 06-24-2017, 05:51 PM
eduzs eduzs is offline Replacing till there's no more double paragraphs in the text Windows 10 Replacing till there's no more double paragraphs in the text Office 2010 32bit
Expert
Replacing till there's no more double paragraphs in the text
 
Join Date: May 2017
Posts: 262
eduzs is on a distinguished road
Default

Thanks gmaxey
Cause I don't now how many ^p^p^p... the text have, my macro should keep changing ^p^p to ^p till there's no more "^p^p", but just "^p", your code seems to do a single find and replace.

Dim oRng As Range

Set oRng = ActiveDocument.Range

Application.ScreenUpdating = False

With oRng.Find

.Text = "^13{2}" (I got a error with "2," so I removed the ,")
.Replacement.Text = "^p"
.MatchWildcards = True
.Execute

End With
__________________
Backup your original file before doing any modification.
Reply With Quote
  #4  
Old 06-24-2017, 05:59 PM
eduzs eduzs is offline Replacing till there's no more double paragraphs in the text Windows 10 Replacing till there's no more double paragraphs in the text Office 2010 32bit
Expert
Replacing till there's no more double paragraphs in the text
 
Join Date: May 2017
Posts: 262
eduzs is on a distinguished road
Default

I returned to this version of my code:

This should be a bug of word vba? I found that the loop/crash ocurrs only when theres a ^p^p at the end of the document.

Sub Limpa_texto()

Application.ScreenUpdating = False

ActiveDocument.Select

With Selection

With .Find

.Text = "^13{2}"
.Replacement.Text = "^13"
.Forward = True
.MatchWildcards = True
.Wrap = wdFindStop
.Execute

End With

Do While .Find.Found

.Find.Execute Replace:=wdReplaceAll

Loop

End With

Application.ScreenUpdating = True

End Sub
__________________
Backup your original file before doing any modification.
Reply With Quote
  #5  
Old 06-24-2017, 06:12 PM
gmaxey gmaxey is offline Replacing till there's no more double paragraphs in the text Windows 7 32bit Replacing till there's no more double paragraphs in the text Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Replace the comma in 2, with whatever the list separator character is for your system.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #6  
Old 06-24-2017, 07:02 PM
eduzs eduzs is offline Replacing till there's no more double paragraphs in the text Windows 10 Replacing till there's no more double paragraphs in the text Office 2010 32bit
Expert
Replacing till there's no more double paragraphs in the text
 
Join Date: May 2017
Posts: 262
eduzs is on a distinguished road
Default

Still get the freeze/crash word when there's "^p^p" at the end of the document.
I'm looking for a workaround to this, also get sometimes "Code execution aborted" message without error.
Maybe instead of search active document use selection.wholestory and move the selection to exclude the last character of the document.
Or add a code to remove the last char of the document when it's "^p^p" before run the rest of the code.
__________________
Backup your original file before doing any modification.
Reply With Quote
  #7  
Old 06-24-2017, 08:20 PM
gmayor's Avatar
gmayor gmayor is offline Replacing till there's no more double paragraphs in the text Windows 10 Replacing till there's no more double paragraphs in the text Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,105
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

The macro doesn't remove an empty paragraph at the end of the document, but that's easily fixed

Code:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
'Graham Mayor - http://www.gmayor.com - Last updated - 25 Jun 2017
Dim oRng As Range
    Set oRng = ActiveDocument.Range
    With oRng.Find
        .Text = "^13{2;}" 'Note the list separator character - here ;
        .Replacement.Text = "^p"
        .MatchWildcards = True
        .Execute Replace:=wdReplaceAll
    End With
    If Len(ActiveDocument.Range.Paragraphs.Last.Range) = 1 Then
        ActiveDocument.Range.Paragraphs.Last.Range.Delete
    End If
lbl_Exit:
    Exit Sub
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
  #8  
Old 06-24-2017, 08:38 PM
eduzs eduzs is offline Replacing till there's no more double paragraphs in the text Windows 10 Replacing till there's no more double paragraphs in the text Office 2010 32bit
Expert
Replacing till there's no more double paragraphs in the text
 
Join Date: May 2017
Posts: 262
eduzs is on a distinguished road
Default

Thanks for helping, based on your idea we got a workaround to this "bug":

Now I can get rid of unwanted paragraphs breaks and repeated blankspaces.

Code:
Sub Limpa_texto()

Dim i As Integer, oRng As Range, aProc, aSubs

aProc = Array("^13{2;}", "^32{2;}", "^13^32")
aSubs = Array("^p", " ", "^p")

Set oRng = ActiveDocument.Range

Do While Len(ActiveDocument.Range.Paragraphs.Last.Range) = 1

    ActiveDocument.Range.Paragraphs.Last.Range.Delete

Loop

Application.ScreenUpdating = False

For i = 0 To UBound(aProc)

    With oRng.Find

        .Text = aProc(i)
        .Replacement.Text = aSubs(i)
        .Forward = True
        .MatchWildcards = True
        .Wrap = wdFindContinue
        .Execute
        
    End With

    Do While oRng.Find.Found

        oRng.Find.Execute Replace:=wdReplaceAll

    Loop

Next i
    
Application.ScreenUpdating = True

End Sub
__________________
Backup your original file before doing any modification.
Reply With Quote
  #9  
Old 06-25-2017, 05:47 AM
gmaxey gmaxey is offline Replacing till there's no more double paragraphs in the text Windows 7 32bit Replacing till there's no more double paragraphs in the text Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Graham, thanks!

Break

eduzs,

I'm not entirely sure that I know what your goal really is, but if it is to remove empty paragraphs and leading or trailing spaces from paragraphs then I think your current version falls short.

And to each his own and perhaps it is due to habit or what you have seen before, but I can't for the life of me see any advantage of your two Do ... While loops or use of the seemingly redundant Find.Found.

Code:
Sub Limpa_texto()
Dim lngIndex As Long, oRng As Range
Dim strLS As String
Dim arrFind, arrReplace
  strLS = Application.International(wdListSeparator)
  arrFind = Array("^32{1,}^13", "^13^32{1,}", "^13{2,}", "^32{2,}")
  arrReplace = Array("^p", "^p", "^p", " ")
  Set oRng = ActiveDocument.Range
  Application.ScreenUpdating = False
  For lngIndex = 0 To UBound(arrFind)
    With oRng.Find
      .Text = Replace(arrFind(lngIndex), ",", strLS)
      .Replacement.Text = Replace(arrReplace(lngIndex), ",", strLS)
      .MatchWildcards = True
      .Execute Replace:=wdReplaceAll
    End With
  Next lngIndex
  If Len(ActiveDocument.Range.Paragraphs.Last.Range) = 1 Then
    ActiveDocument.Range.Paragraphs.Last.Range.Delete
  End If
  Application.ScreenUpdating = True
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #10  
Old 06-25-2017, 05:52 AM
eduzs eduzs is offline Replacing till there's no more double paragraphs in the text Windows 10 Replacing till there's no more double paragraphs in the text Office 2010 32bit
Expert
Replacing till there's no more double paragraphs in the text
 
Join Date: May 2017
Posts: 262
eduzs is on a distinguished road
Default

Thanks!
I explain, in the text I have "pp", "ppp", "ppppp".... I don't know how many in sequence, and I need to change all this to a only one "p", so I (by what I know) can't do a single find and replace "pp" to "p".
So the code is keeping "pppp", "ppppp" in the document
Is there a wildcard that replace a unknown number sequence of repeated "^p" to a single "^p", or, another aproach, delete all len(p)=1 paragraphs?
__________________
Backup your original file before doing any modification.
Reply With Quote
  #11  
Old 06-25-2017, 05:59 AM
gmaxey gmaxey is offline Replacing till there's no more double paragraphs in the text Windows 7 32bit Replacing till there's no more double paragraphs in the text Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

I had my doubts that you had tested the code before. If you had you might have been left with two paragraphs at the end of the document but Word would not have crashed or been stuck in a loop.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #12  
Old 06-25-2017, 06:13 AM
eduzs eduzs is offline Replacing till there's no more double paragraphs in the text Windows 10 Replacing till there's no more double paragraphs in the text Office 2010 32bit
Expert
Replacing till there's no more double paragraphs in the text
 
Join Date: May 2017
Posts: 262
eduzs is on a distinguished road
Default

Your code works fine, but I'm testing it in a complex html page pasted in word, I'm working to know what's the problem.
Thanks a lot for helping.

what's means "{2,}" ?
__________________
Backup your original file before doing any modification.
Reply With Quote
  #13  
Old 06-25-2017, 06:28 AM
gmaxey gmaxey is offline Replacing till there's no more double paragraphs in the text Windows 7 32bit Replacing till there's no more double paragraphs in the text Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

It means two or more instances of the preceding defined character
e.g., a{2,} will find aa, aaa, aaaa, etc., but it won't find a.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #14  
Old 06-25-2017, 06:31 AM
eduzs eduzs is offline Replacing till there's no more double paragraphs in the text Windows 10 Replacing till there's no more double paragraphs in the text Office 2010 32bit
Expert
Replacing till there's no more double paragraphs in the text
 
Join Date: May 2017
Posts: 262
eduzs is on a distinguished road
Default

I found just a minor problem, when ^p^p^p... is followed by a table it's not replacing these occurrences.
__________________
Backup your original file before doing any modification.
Reply With Quote
  #15  
Old 06-25-2017, 06:43 AM
gmaxey gmaxey is offline Replacing till there's no more double paragraphs in the text Windows 7 32bit Replacing till there's no more double paragraphs in the text Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Code:
Sub Limpa_texto()
Dim lngIndex As Long, oRng As Range
Dim strLS As String
Dim arrFind, arrReplace
  strLS = Application.International(wdListSeparator)
  arrFind = Array("^32{1,}^13", "^13^32{1,}", "^13{2,}", "^32{2,}")
  arrReplace = Array("^p", "^p", "^p", " ")
  Set oRng = ActiveDocument.Range
  Application.ScreenUpdating = False
  For lngIndex = 0 To UBound(arrFind)
    With oRng.Find
      .Text = Replace(arrFind(lngIndex), ",", strLS)
      .Replacement.Text = Replace(arrReplace(lngIndex), ",", strLS)
      .MatchWildcards = True
      .Execute Replace:=wdReplaceAll
    End With
  Next lngIndex
  'Deal with dispersed tables and final paragraph.
  For lngIndex = ActiveDocument.Range.Paragraphs.Count To 1 Step -1
    If Len(ActiveDocument.Range.Paragraphs(lngIndex).Range.Text) = 1 Then
      ActiveDocument.Range.Paragraphs(lngIndex).Range.Delete
    End If
  Next
  Application.ScreenUpdating = True
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Replacing till there's no more double paragraphs in the text Help with replacing text with wildcards sbatson5 Word 2 04-13-2012 03:49 AM
Why the "Text boundaries" are not shown till the end of the page while in other file Jamal NUMAN Word 4 03-28-2012 07:58 AM
Replacing till there's no more double paragraphs in the text outlook double spacing paragraphs GWBDIRECT Outlook 3 04-06-2011 11:29 AM
Replacing till there's no more double paragraphs in the text Replacing a single "l" with a double "ll" MShroff Word 8 01-19-2011 08:43 AM
Replacing / editting text LisaC Word 0 02-25-2010 03:40 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:57 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