Hey, I'm trying to write MS word VBA code to find a space to the left of the cursor. If the space is found, go to right of the space, otherwise, type a space. I've also attached an image to try to be more specific.
My code assumes I start to the left of the required location. I then select the text one character to the left. I search this text for a " ". However, I can't get it to work correctly.
Could anybody please help me get my code, shown below, sorted?
Code:
Sub IfSpaceFound()
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
With Selection.Find
.ClearFormatting
.Text = " "
.Forward = True
.Wrap = False
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
If .Found
Selection.MoveRight Unit:=wdCharacter, Count:=1
Else
Selection.TypeText Text:=" "
End If
End With
End Sub