Hi,
I've been trying to make a Form app that will open a Word doc and then search through it using the rows in an Access database - if it finds the text in column 1, it replaces it with the text in column 2. But...something keeps going wrong! Very new to VB.NET so there may be something very obvious I'm missing.
If I use this code, when the doc opens it's clear something is happening as the blue spinning wheel is going, but then the process ends and nothing has been replaced -
Code:
For i = 1 To dtset.Tables("ReplacementSet").Rows.Count
Dim objDoc As Document = objWord.Documents.Open(TextBox2.Text)
Dim objRng As Range = objDoc.Range ' Would help?
Dim rFindText As String = dtset.Tables("ReplacementSet").Rows(i).Item(0).ToString
Dim rReplacementText As String = dtset.Tables("ReplacementSet").Rows(i).Item(1).ToString
objWord.Selection.HomeKey(Word.WdUnits.wdStory)
With objRng.Find
.ClearFormatting()
.Forward = True
.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue
.MatchCase = True
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
.Text = rFindText
.Replacement.Text = rReplacementText
End With
Next i
If I replace the part of the code which executes the find and replace command, when Word opens I get an error warning saying "Value is out of range"
Code:
objRng.Find.Execute(FindText:=rFindText, MatchCase:=True, MatchWholeWord:=False, MatchWildcards:=True, MatchSoundsLike:=False, MatchAllWordForms:=False, Forward:=True, Wrap:=True, Format:=False, ReplaceWith:=rReplacementText, Replace:=True, MatchKashida:=False, MatchDiacritics:=False, MatchAlefHamza:=False, MatchControl:=False)
Next i
Same thing happens if I use objDoc.Content instead of objRange. Does anybody have any ideas? I feel like I'm close, but maybe not!