Automating Word - Problem with .Find

  • Thread starter Thread starter Al_P via DotNetMonster.com
  • Start date Start date
A

Al_P via DotNetMonster.com

I have:
Win2K
Office2000
Working in VB.Net (2003)
My .Net project has a reference to Word 9.0

Instantiating things in a simple and straightforward manner:
Dim word as Word.Application
Dim doc As Word.Document
Dim range As Word.Range
Dim selection As Word.Selection

word = New Word.Application
doc = word.Documents.Add("<path to a .dot>")

Everything works fine, until I try to do a Find:
range.Find.Text = "Your string here"

The Find object returns a NullReferenceException. I've tried every
variation I can think of using Find, including using the Selection.Find
object. The same code works okay in VB6, although that's probably
irrelevent except for verifying my syntax is okay.

I've Googled this issue to the point of exhaustion, and haven't been able
to find an answer. Anybody have any ideas?

Thanks
 
I have:
Win2K
Office2000
Working in VB.Net (2003)
My .Net project has a reference to Word 9.0

Instantiating things in a simple and straightforward manner:
Dim word as Word.Application
Dim doc As Word.Document
Dim range As Word.Range
Dim selection As Word.Selection

word = New Word.Application
doc = word.Documents.Add("<path to a .dot>")

Everything works fine, until I try to do a Find:
range.Find.Text = "Your string here"

The Find object returns a NullReferenceException. I've tried every
variation I can think of using Find, including using the Selection.Find
object. The same code works okay in VB6, although that's probably
irrelevent except for verifying my syntax is okay.

I've Googled this issue to the point of exhaustion, and haven't been able
to find an answer. Anybody have any ideas?

Thanks

you haven't set range to anything. Try
range = doc.content

example (find text "find text" and replace with "replacement text" in
test.doc)


Dim word As Word.Application
Dim doc As Word.Document
Dim range As Word.Range
word = New Word.Application
doc = word.Documents.Add("test.doc")
word.Visible = True
range = doc.Content
With range.Find
.ClearFormatting()
If range.Find.Execute(FindText:="find text") Then
range.Text = "replacement text"
End If
End With


When range finds something the range is set to that something, which is
why the replacement text is assigned to range.text. If you want to find
again you need to reset the range to the document content.
 
Yes- thanks for the note. You're right about the range object.

The underlying problem, which I discovered this morning, was a corrupted
Word typelib (msword9.olb). If you care to read further, go to Microsoft
and find Article 292744.

Thanks for the reply.
 

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

Back
Top