Word VBA code for moving the cursor with a ‘find’

The following VBA code will …

  • Search a Word document for “asdf”
  • Position the cursor immediately after “asdf”
  • Add the letters “jkl;” at the new cursor point
  • Move the cursor back four spaces, leaving it immediately after “asdf”

Sub move_cursor_with_find()
With Selection.Find
.Text = "asdf"
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = True
End With
Selection.Find.Execute
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeText Text:="jkl;"
Selection.MoveLeft Unit:=wdCharacter, Count:=4
End Sub

 

– Eric DeRosia