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 / December 2007

Tip: Looking for answers? Try searching our database.

find and replace text that is not in a table

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
MikeB77 - 16 Dec 2007 10:21 GMT
Hello
I'm trying to tidy up word text by removing or applying double spaces after
sentences. the selected text to tidy MAY include tables But I don't want to
change text that is in a tables - just text outside tables.

How can I limit .find to non-table text.?  I've stumbled across
range.information(wdWithInTable) as a lead but can weave this in.
Or could I redefine my myRng text to exclude tables?

my snippets so far:

Sub mySub
'set myRng from selection or story.. it depends
'get A which is either ' ' or '  ' depending on if proportional font or not
'call my function (below) for various sentence ending punctuation:
MakeChanges4(myRng, ".", ".", A)
MakeChanges4(myRng, ":", ":", A)
MakeChanges4(myRng, "[\!]", "!", A)
MakeChanges4(myRng, "[\?]", "?", A)
end Sub

Private Function MakeChanges4(myRange As Range, findPunct As String, punct
As String, newSpace As String)
   sp = "[ ]{1,}" 'spaces string using wildcard format
   
           myRange.Find.ClearFormatting

' MY QUESTION HERE - HOW TO LIMIT THIS TO REPLACE ONLY NON TABLE TEXT        
     
           myRange.Find.Execute FindText:=findPunct & sp, _
             ReplaceWith:=punct & newSpace, MatchCase:=0, _
             Replace:=wdReplaceAll, MatchWildcards:=True
             
End Function

Thanks in advance

Mike
Helmut Weber - 16 Dec 2007 12:27 GMT
Hi Mike,

there is no straightforward way, IMHO.

Unless you want to use some workaround,
like coloring the text in the tables beforehand
and search e.g. for text whith color automatic,
or hide the tables, which I would not recommend,
you'd have to check each found spot, like:

Sub Testx()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
With rDcm.Find
  .Text = "Fox"
  While .Execute
      If Not rDcm.Information(wdWithInTable) Then
         rDcm.Text = "X"
         rDcm.Collapse Direction:=wdCollapseEnd
      End If
  Wend
End With
End Sub

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
MikeB77 - 17 Dec 2007 09:41 GMT
Thanks Helmut
with your code and the additon of .MatchWildcards = True I've got a solution.

> Hi Mike,
>
[quoted text clipped - 27 lines]
>
> Vista Small Business, Office XP
Greg Maxey - 16 Dec 2007 15:27 GMT
Mike,

I concur with Helmut.  I have the following code posted on my website:

Sub TwoSpacesAfterSentence2()
Dim oRng As Range
Set oRng = ActiveDocument.Range
With oRng.Find
   .ClearFormatting
   .MatchWildcards = True
   .Text = "(*{2})([.\!\?]) ([A-Z])"
   .Replacement.Text = "\1\2  \3" 'Two spaces between 2 and \
   .Execute Replace:=wdReplaceAll
   .Text = "([.\!\?]) {3,}([A-Z])"
   .Replacement.Text = "\1  \2"
   .Execute Replace:=wdReplaceAll
   'This should prevent most cases of improper double spacing
   'in names (e.g., F. Lee Bailey, George W. Bush, etc.)
   .Text = "([!A-Z][A-Z].)  ([A-Z])" 'Two spaces between ) and (
   .Replacement.Text = "\1 \2"
   .Execute Replace:=wdReplaceAll
End With
End Sub

I don't know why you don't want to process tables, but if it is due to
having a bunch of names or something similiar in the cells then this may
work for you.  Regardless you need to be aware that your code as written
code adversly effect the look of names like John F. Kennedy.

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

> Hello
> I'm trying to tidy up word text by removing or applying double spaces
[quoted text clipped - 37 lines]
>
> Mike
MikeB77 - 17 Dec 2007 09:48 GMT
Thanks Greg. my goal is to make it either single or double spacing after
sentences (proportional font or not). The more I think about it I'm not that
sure why I'm worried about tables - I think I've formatted some so poorly in
the past that I'm keen not to mess with past messes. With Helmut's individual
test I can also open this up to including/excluding headings and then do the
spaces between words more easily.

> Mike,
>
[quoted text clipped - 66 lines]
> >
> > Mike
 
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.