Conditional Find / Replace Challenge

J

justme

Hi,

I need to know how I can search a whole document to replace every \^p
(backslash and paragraph return), EXCEPT if it is immediately followed by 5
numbers, 1 letter, and a pipe character (|).

Is this possible?

Thank you.
 
H

Helmut Weber

Hi,

something along these lines:

Sub Test0034()

Dim s As String
Dim i As Long
Dim bFound As Boolean
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "\\^0013"
.MatchWildcards = True
While .Execute
bFound = True
rDcm.Select ' for testing, remove later
For i = 1 To 5
rDcm.End = rDcm.End + 1
rDcm.Select ' for testing, remove later
s = rDcm.Characters.Last
If Not IsNumeric(s) Then
bFound = False
End If
Next
rDcm.End = rDcm.End + 1
rDcm.Select ' for testing, remove later
s = rDcm.Characters.Last
If Not LCase(s) <> UCase(s) Then ' letter
bFound = False
End If
rDcm.End = rDcm.End + 1
rDcm.Select ' for testing, remove later
s = rDcm.Characters.Last
If s <> "|" Then
bFound = False
End If
If bFound = True Then
rDcm.Text = "xxxxxxxx" 'replacement
End If
rDcm.start = rDcm.End
rDcm.End = ActiveDocument.Range.End
Wend
End With
End Sub

If you are sure that all chr(13) are paragaph marks,
which isn't always the case, then you could do it whithout wildcards,
and search for \^p.


HTH

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
H

Helmut Weber

.... and a million ways for improvement

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
R

Russ

Hi Justme,
I need to know how I can search a whole document to replace every \^p
(backslash and paragraph return), EXCEPT if it is immediately followed by 5
numbers, 1 letter, and a pipe character (|).

Is this possible?

Thank you.
Another way to do things would be to hide or mark characters in the first
Find(s) and Replace(s) that you don't want to find in the subsequent Find(s)
and Replace(s). Depending on the document layout this method might be
faster. For example:

Dim aR As Range
Set aR = ActiveDocument.Content
With aR.Find
.MatchWildcards = True
.Text = "\\^13[0-9]{5}[a-zA-Z]|" 'use \n for ^13 in MacWord
.Replacement.Text = "^&"
.Replacement.Font.hidden = True
.Execute replace:=wdReplaceAll

.Font.hidden = False
.Replacement.Font.hidden = False
.Text = "\\^13"
.Replacement.Text = "XXXX^&" 'for example
.Execute replace:=wdReplaceAll
End With
ActiveDocument.Content.Font.hidden = False

Hiding things is also good for exposing only a particular random-positioned
pattern in all paragraphs that you want to do a paragraph sort on.
First hide everything.
Second expose sort pattern.
After sorting then unhide everything.
For example:

Dim show_hide As Boolean

show_hide = ActiveWindow.ActivePane.View.ShowAll
ActiveWindow.ActivePane.View.ShowAll = True
ActiveDocument.Range.Font.hidden = True 'hide everything
Dim aR As Range
Set aR = ActiveDocument.Content
With aR.Find
.MatchWildcards = True
.Text = "XXXXXXX" 'sort pattern exposure
.Replacement.Text = "^&"
.Font.hidden = True
.Replacement.Font.hidden = False
.Execute replace:=wdReplaceAll
End With
ActiveWindow.ActivePane.View.ShowAll = False
ActiveDocument.Range.Sort ExcludeHeader:=False, FieldNumber:="Paragraphs", _
SortFieldType:=wdSortFieldNumeric, SortOrder:=wdSortOrderAscending
ActiveDocument.Range.Font.hidden = False
ActiveWindow.ActivePane.View.ShowAll = show_hide

A direct paragraph sort crashes a Word 97 computer at work. One way to get
around the crash on that computer is to convert the paragraphs into a table
and sort the table, then convert the sorted table back into paragraphs.
Which can be done via VBA within the subroutine.

Try
ActiveDocument.Range.TextRetrievalMode.IncludeHiddenText = False
If hidden text is not ignored during Find and Replace.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top