spebola Posted August 7, 2003 Posted August 7, 2003 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. Quote
Diablicolic Posted August 7, 2003 Posted August 7, 2003 I think I've got what the doctor ordered, here's an example I've picked up for replacing strings from a rich text box: 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 Quote "Reality is fake, Dreams are for real"
*Experts* Volte Posted August 7, 2003 *Experts* Posted August 7, 2003 That looks like it just finds lines which contain the word "testing" and removes them. The problem with spebola's code is that he is not initializing the object variables.Dim wrdApp As Word._Application Dim wrdDoc As Word.Document Dim oRg As Word.RangeThat creates references, but no actual object.Dim wrdApp As New Word._Application Dim wrdDoc As New Word.Document Dim oRg As New Word.RangeI'm not that familiar with Office Automation, but I think that will probably stop that error. Also, I don't 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. Quote
spebola Posted August 7, 2003 Author Posted August 7, 2003 (edited) Thanks for the responses. The NEW keyword is not allowed for word.document or word.range objects. I don't understand why the error appears at the .Wrap statement instead of at .ClearFormatting if the object does not exist. Edited August 7, 2003 by spebola Quote
*Experts* Volte Posted August 7, 2003 *Experts* Posted August 7, 2003 Are you sure the error message is referring to 'oRg.Wrap' and not the second part of the line 'Word.WdFindWrap.wdFindContinue'? Quote
spebola Posted August 7, 2003 Author Posted August 7, 2003 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. Quote
alexhitchins Posted October 28, 2003 Posted October 28, 2003 Try putting Set oRg = wrdDoc.Range Instead of oRg = wrdDoc.Range This works on my machne. Quote
fcoreyesv Posted October 30, 2003 Posted October 30, 2003 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. 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 Quote
fcoreyesv Posted October 30, 2003 Posted October 30, 2003 the paramenter 2 is this constant Word.WdReplace.wdReplaceAll so the line should be like this: bFound = mySelection.Find.Execute(txtFind.Text, , True, , , , , , , txtReplace.Text, Word.WdReplace.wdReplaceAll) Greetings Jose Reyes Quote
sandeepca Posted December 23, 2003 Posted December 23, 2003 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 developer's machine but it doesn't work from one particular machine. It says "Object reference not set" as soon as it hit wdFindWrap. Don't know why. If I find the reason would let you know. Thanks Quote
Recommended Posts