Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / April 2007

Tip: Looking for answers? Try searching our database.

Conditional Find / Replace Challenge

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
justme - 08 Apr 2007 09:24 GMT
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.
Helmut Weber - 08 Apr 2007 10:43 GMT
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

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Helmut Weber - 08 Apr 2007 10:45 GMT
... and a million ways for improvement

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Russ - 10 Apr 2007 03:31 GMT
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
[quoted text clipped - 3 lines]
>
> 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.

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.