Search for a word in a document except for tables?

J

Jun

How do you search the whole document, excluding tables, for a certain word?
And vice versa (just search tables)?

Thanks.
 
H

Helmut Weber

Hi,
IMHO not possible in a straightforward way.
You may search tables only, but not the rest of the doc only.
However, you may determine, whether the first found string (!),
is inside a table or not, like:
Sub Test444()
Dim rdcm As Range
Set rdcm = ActiveDocument.Range
Resetsearch
With rdcm.Find
.Text = "Teil"
If .Execute And rdcm.Information(wdWithInTable) = True Then
MsgBox "found inside of a table"
Else
MsgBox "found outside of a table"
End If
End With
End Sub
Sub Resetsearch()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
End Sub
---
The whole truth would be to count how many occurences of a string
are inside tables and how many are outside.
If you want help for that too, ask again.
---
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
H

Helmut Weber

Well...,
one could define ranges which encompass the text
between start of document, the next table, if existing,
between tables, if there are any, and between the last table,
if there is a table at all, and the end of the doc,
which wouldn't make it much easier ;-)
---
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
J

Jean-Guy Marcil

Helmut Weber was telling us:
Helmut Weber nous racontait que :
Hi,
IMHO not possible in a straightforward way.
You may search tables only, but not the rest of the doc only.
However, you may determine, whether the first found string (!),

Why the first one only?

This slight modification to your code does the rick, no? What am I missing?

'_______________________________________
Sub Test444()
Dim rdcm As Range
Set rdcm = ActiveDocument.Range
Resetsearch

With rdcm.Find
.Text = "Teil"
Do While .Execute
If rdcm.Information(wdWithInTable) Then
MsgBox "found inside of a table"
rdcm.Font.Color = wdColorBlue
Else
MsgBox "found outside of a table"
rdcm.Font.Color = wdColorRed
End If
Loop
End With

End Sub
'_______________________________________

'_______________________________________
Sub Resetsearch()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
End Sub
'_______________________________________

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
H

Helmut Weber

Hi Jean-Guy,
of course, you are not missing anything.
What I meant was that I don't see a straightforward way,
to exclude tables from being searched at all.
While one could search all table ranges and
and thus exclude the rest of the document,
the other way round would need quite a bit more coding.
However, you may determine, whether the first found string (!),
Why the first one only?
I just wanted to point at the limitations of my example,
and as it doesn't answer the question in a strict sense,
tried only to show how to use information(wdWithInTable).
Have a nice day.
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
J

Jean-Guy Marcil

Helmut Weber was telling us:
Helmut Weber nous racontait que :
Hi Jean-Guy,
of course, you are not missing anything.
What I meant was that I don't see a straightforward way,
to exclude tables from being searched at all.
While one could search all table ranges and
and thus exclude the rest of the document,
the other way round would need quite a bit more coding.
I just wanted to point at the limitations of my example,
and as it doesn't answer the question in a strict sense,
tried only to show how to use information(wdWithInTable).

Always the perfectionist! ;-)
Indeed, it would take some detours to exclude tables from a search...

But, the poster got what he wanted... and that's the important part!

Cheers!

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
P

Poseur

Hi,
IMHO not possible in a straightforward way.
You may search tables only, but not the rest of the doc
only.

I was just looking at the help file for Word VBA 2003. I don't
have 2003 so I have not been able to check this out, but there
are some new objects and properties that may possibly make it
more straightforward:

Rectangle Object: Represents a portion of text or a graphic in a
page.
RectangleType Property: ...wdTextRectangle Represents a space
occupied by text.

Line Object: Represents an individual line in a Rectangle object
of type wdTextRectangle.
LineType Property: Returns a wdLineType constant (wdTableRow or
wdTextLine)

So, maybe...
If Not myRectangle.Lines(i).LineType = wdTableRow Then ...

I don't know. Don't really have a good sense of what a rectangle
is or whether Rectangle or Line can have a Find property.
Apparently, Rectangle is not a range or child of range.
 

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