By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,498 Members | 1,564 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,498 IT Pros & Developers. It's quick & easy.

Automating Word - Problem with .Find

P: n/a
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

--
Message posted via http://www.dotnetmonster.com
Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"Al_P via DotNetMonster.com" <fo***@DotNetMonster.com> wrote in
news:ee******************************@DotNetMonste r.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


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.
Nov 21 '05 #2

P: n/a
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.

--
Message posted via http://www.dotnetmonster.com
Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.