Automating Word - Problem with .Find

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Al_P via DotNetMonster.com

    Automating Word - Problem with .Find

    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.Applicatio n
    Dim doc As Word.Document
    Dim range As Word.Range
    Dim selection As Word.Selection

    word = New Word.Applicatio n
    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 NullReferenceEx ception. 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
  • jo0ls

    #2
    Re: Automating Word - Problem with .Find

    "Al_P via DotNetMonster.c om" <forum@DotNetMo nster.com> wrote in
    news:ee132c345a 0d48a2adfe46b3b 597382a@DotNetM onster.com:
    [color=blue]
    > 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.Applicatio n
    > Dim doc As Word.Document
    > Dim range As Word.Range
    > Dim selection As Word.Selection
    >
    > word = New Word.Applicatio n
    > 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 NullReferenceEx ception. I've tried every
    > variation I can think of using Find, including using the[/color]
    Selection.Find[color=blue]
    > 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[/color]
    able[color=blue]
    > to find an answer. Anybody have any ideas?
    >
    > Thanks
    >[/color]

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

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


    Dim word As Word.Applicatio n
    Dim doc As Word.Document
    Dim range As Word.Range
    word = New Word.Applicatio n
    doc = word.Documents. Add("test.doc")
    word.Visible = True
    range = doc.Content
    With range.Find
    .ClearFormattin g()
    If range.Find.Exec ute(FindText:=" find text") Then
    range.Text = "replacemen t 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.

    Comment

    • Al_P via DotNetMonster.com

      #3
      Re: Automating Word - Problem with .Find

      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

      Comment

      Working...