Problem with Find and Tables

E

Erik Gollot

I've made a script that search all words of a specified style.
This macro works fine but enter in an infinite loop when it find a text
with the specified style into a table

Any idea ?

Here is the script :
Set WSHShell = WScript.CreateObject("WScript.Shell")
Set appWord = Wscript.CreateObject("Word.Application")
appWord.Documents.Open("C:\users\erik\bnpp\search.doc")
appWord.Visible = False
appWord.Selection.Find.ClearFormatting
With appWord.Selection.Find
.Text= ""
.Style = appWord.ActiveDocument.Styles("Exigence")
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
Do While appWord.Selection.Find.Execute = True
MsgBox appWord.Selection
Loop
End With
appWord.Quit
Set appWord = Nothing
 
H

Helmut Weber

Hi Erik,

like that, tested from Excel:

Sub xTest()
Set oWrd = GetObject(, "Word.Application")
Dim rTmp As Object
' which is no good, but other ways don't work here and now

oWrd.Documents.Open ("C:\test\test.doc")
oWrd.Visible = False
Set rTmp = oWrd.activedocument.Range
With rTmp.Find
.Style = oWrd.activedocument.Styles("Heading 1")
While .Execute
MsgBox rTmp
rTmp.collapse Direction:=wdcollapseend ' (!)
On Error Resume Next
If rTmp.Characters.last.Next = Chr(13) And _
rTmp.Characters.last.Next.Next = Chr(7) Then
End If
Wend
End With
oWrd.Visible = True
End Sub

The on-error command could be avoided,
but I wanted to keep it rather simple.

Make sure, that there are at least 2 characters
behind every found instance of text of the style
you are looking for,
otherwise one would have to find out, whether
rTmp.Characters.last.Next.Next
is not nothing.

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
E

Erik Gollot

sorry but it does not help me :-(
Do not take 'Heading 1' as an example because you "never" use it into a
table cell. My sample is with "Exigence" (i.e. "Requirement" in english)
My problem comes when some words into a table cell (a word table) has the
required style.
The macro enters into an infinite loop.
If before this table I've many words with the specified style, everything is
ok, the problem is with a table.
What is strange is that under word with the interactive search, word can
find all the words with the style "Exigence", even the words into table cells
!!??

Any new idea ?
 
K

Klaus Linke

Hi Eric,

Which version?

Word 2007 has some bugs in Find -- especially involving tables -- where Word
goes off into an endless loop.
Some of them seem to be fixed if you install a hot fix:
http://support.microsoft.com/kb/955587

But that does not fix all issues...

Sometimes you can work around the bugs by searching bottom to top.
Or, since the bugs seem to be caused by paragraph markers and
end-of-cell-markers in the matched text, you might try a wildcard search for
[!^13]@ (and the paragraph style you're interested in).

Regards,
Klaus
 
E

Erik Gollot

Problem solved
the magic command is appWord.Selection.Moveright

Set WSHShell = WScript.CreateObject("WScript.Shell")
Set appWord = Wscript.CreateObject("Word.Application")
appWord.Documents.Open("C:\data\work\RING\Exigences dans
word\doc_avec_styles.doc")
appWord.Visible = FALSE
appWord.Selection.Find.ClearFormatting
With appWord.Selection.Find
.Text= ""
.Forward = True
.Style = appWord.ActiveDocument.Styles("Exigence")
Do While appWord.Selection.Find.Execute = True
MsgBox appWord.Selection
appWord.Selection.Moveright
Loop
End With
appWord.Quit
Set appWord = Nothing

Klaus Linke said:
Hi Eric,

Which version?

Word 2007 has some bugs in Find -- especially involving tables -- where Word
goes off into an endless loop.
Some of them seem to be fixed if you install a hot fix:
http://support.microsoft.com/kb/955587

But that does not fix all issues...

Sometimes you can work around the bugs by searching bottom to top.
Or, since the bugs seem to be caused by paragraph markers and
end-of-cell-markers in the matched text, you might try a wildcard search for
[!^13]@ (and the paragraph style you're interested in).

Regards,
Klaus




Erik Gollot said:
sorry but it does not help me :-(
Do not take 'Heading 1' as an example because you "never" use it into a
table cell. My sample is with "Exigence" (i.e. "Requirement" in english)
My problem comes when some words into a table cell (a word table) has the
required style.
The macro enters into an infinite loop.
If before this table I've many words with the specified style, everything
is
ok, the problem is with a table.
What is strange is that under word with the interactive search, word can
find all the words with the style "Exigence", even the words into table
cells
!!??

Any new idea ?
 
K

Klaus Linke

Problem solved
the magic command is appWord.Selection.Moveright

Glad you solved it... I should have tested your code.

If the current selection matches the Find conditions (... the style in your
case), Word should automatically continue searching the rest of the
document.
It's what usually happens, at least -- but it doesn't seem to work properly
for your search in tables.

Regards,
Klaus
 

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