I would like to do a replace all using VBnet 2003 and MSword 2007.
I got to this
Dim Selection As Word.Selection
Selection.Find.ClearFormatting()
Selection.Find.Replacement.ClearFormatting()
Selection.Find.Replacement.Font.Underline = Word.WdUnderline.wdUnderlineSingle
With Selection.Find
.Text = "Text"
.Replacement.Text = "Replacement"
.Forward = True
.Format = True
.Wrap = Word.WdFindWrap.wdFindContinue
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
.MatchFuzzy = False
End With
Selection.Find.Execute(Replace:=Word.WdReplace.wdReplaceAll)
It crashes EVERY line with the selection claiming that “Object reference not set to an instance of an object.” I got the code by doing a macro recording and it worked fine as a macro but I do not want macros in my document. How do I fix this?
First you need to link to Word and the document. This code assumes Word is running with the correct document loaded and displayed.