Search Function crashes every time I use it

M

MikeZz

I should preface by saying I'm running this routine from Excel 2003 VBA but
I'm not sure if that will make a difference in the answer.

When ever I run this sub, and get to line: .Text = sText , it causes Excel
to immediatly crash and re-start.

All I'm trying to do is get the Start & End Locations of a string in a long
word document. I'd also like the option of getting the next table #
following the string as indicated with the optional variable: GetNextTable.

Just an FYI, my word VBA experience is zero so if you can help, please use
full & correct syntax. If there's an error, I won't have a clue how to find
it.

Any help would be GREATLY appreciated!

Thanks
MikeZz



Private Sub w01_Find_This_String(sText As String, oRg As Word.Range,
Optional GetNextTable)

Dim oTable As Table

Set oRg = ActiveDocument.Range
With oRg.Find
.ClearFormatting
.Text = sText
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = False

Do While .Execute
oRg.Collapse wdCollapseEnd
Loop
End With

End Sub
 
T

Tony Jollans

Without checking in great detail, the first thing to do is make sure all
your objects are correctly declared ...

....
Dim oTable As Word.Table

Set oRg = (ref to Word object).ActiveDocument.Range
...

Your code doesn't show how you access Word, or store a reference to it; you
may need to add it as an extra argument to the procedure.
 
M

MikeZz

Hi Tony,
I ended up using the Instr(1,oWord.Range.text,"My String") and it seemed to
work good.... and also a lot quicker using a find.

Thanks for the help,
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