Word Find/Replace question

spebola

Well-known member
Joined
Mar 9, 2003
Messages
50
Location
Oklahoma City
I am trying to do a find/replace text in a word document. The code is:

Dim wrdApp As Word._Application
Dim wrdDoc As Word.Document
Dim oRg As Word.Range
Dim mFind As String
Dim mReplText As String
Dim FileName As String

mFind = "<Agreement Date>"
mReplText = Format(mdy, "MMMM dd, yyyy")

wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
wrdDoc = wrdApp.Documents.Open(FileName)
wrdDoc.Select()
oRg = wrdDoc.Range

With oRg.Find
.ClearFormatting()
.Forward = True
.Wrap = Word.WdFindWrap.wdFindContinue
.MatchCase = False
.Text = strFind
.Replacement.Text = strReplace
.Execute(Replace:=Word.WdReplace.wdReplaceAll)
End With

When this code is executed I get an error at the .Wrap statement. Error message "Object reference not set to an instance of an object". What step am I leaving out? I am using vb.net 2003 professional and Office XP.

Any comments would be appreciated.
 
I think Ive got what the doctor ordered, heres an example Ive picked up for replacing strings from a rich text box:

[VB]
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim x As Integer = RichTextBox1.Lines.GetLowerBound(0)
Dim y As Integer = RichTextBox1.Lines.GetUpperBound(0)

Dim i As Integer

For i = x To y
If InStr(RichTextBox1.Lines(i), "testing") Then /// your chosen words to seek in a line where it says testing
RichTextBox1.Text = Replace(RichTextBox1.Text, RichTextBox1.Lines(i).ToString(), "")
End If
Next

End Sub
[/VB]
 
That looks like it just finds lines which contain the word "testing" and removes them.

The problem with spebolas code is that he is not initializing the object variables.
Code:
Dim wrdApp As Word._Application
Dim wrdDoc As Word.Document
Dim oRg As Word.Range
That creates references, but no actual object.
Code:
Dim wrdApp As New Word._Application
Dim wrdDoc As New Word.Document
Dim oRg As New Word.Range
Im not that familiar with Office Automation, but I think that will probably stop that error.

Also, I dont think that CreateObject line is required after you use the New keyword. If problems ensue, remove the New from the wrpApp declaration line and continue to do it using the CreateObject function.
 
Thanks for the responses.

The NEW keyword is not allowed for word.document or word.range objects.

I dont understand why the error appears at the .Wrap statement instead of at .ClearFormatting if the object does not exist.
 
Last edited by a moderator:
Are you sure the error message is referring to oRg.Wrap and not the second part of the line Word.WdFindWrap.wdFindContinue?
 
Yes, I am sure it refers to oRg.Wrap. I moved the .Wrap line below the .MatchCase line, and the same error occurs on the .MatchCase line. But the first two (.Clearformatting & .Forward) do not generate the error.
 
casually I am working with a similar project and I was struggling with the same problem. Somehow I made it work today.

This is my code. try to adapt it to your needs. as you can see I am not using Createobject.

Code:
Dim WordApp As Word.Application
Dim Worddoc As Word.Document WordApp = New Word.Application

Worddoc = WordApp.Documents.Open("c:\cypher\delitesting.doc")
Dim mySelection As Word.Range
WordApp.Visible = True
Dim bFound As Boolean

mySelection = Worddoc.Range

bFound = True

bFound = mySelection.Find.Execute(txtFind.Text, , True, , , , , , , txtReplace.Text, 2)

Instead of using the "With myselection.find" I am passing all the parameters to the execute function.

The parameter 2 is the replace mode, by good luck I figured out that the value 2 is iqual to wdReplaceAll from VBA .
The value "True" is the Matchwholeword parameter.

I hope it helps. My program is working now.

Cheers
 
the paramenter 2 is this constant Word.WdReplace.wdReplaceAll so the line should be like this:

Code:
bFound = mySelection.Find.Execute(txtFind.Text, , True, , , , , , , txtReplace.Text, Word.WdReplace.wdReplaceAll)

Greetings
Jose Reyes
 
spebola,

Did you find the solution to your problem or you had to change code?

I am having exactly same problem, but the application works from my machine and other developers machine but it doesnt work from one particular machine.

It says "Object reference not set" as soon as it hit wdFindWrap.

Dont know why. If I find the reason would let you know.

Thanks
 
Back
Top