find and replace text that is not in a table

M

MikeB77

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
 
H

Helmut Weber

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
 
G

Greg Maxey

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.
 
M

MikeB77

Thanks Helmut
with your code and the additon of .MatchWildcards = True I've got a solution.
 
M

MikeB77

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.

Greg Maxey said:
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.

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

MikeB77 said:
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
 

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