Dtermine character in front of cursor position?

M

ML

I am creating a custom form that allows the user to select an autotext entry
based on conditions they select on the form. I want to insert the autotext
item at the current cursor position using:

ActiveDocument.AttachedTemplate.AutoTextEntries("Blah").Insert
Where:=Selection.Range, RichText:=True

However if there is a character directly to the left of the cursor I need to
add a space and if one to the right I need to add a space.

Is there some way to determine this from the current cursor position?
 
J

Jay Freedman

ML said:
I am creating a custom form that allows the user to select an
autotext entry based on conditions they select on the form. I want
to insert the autotext item at the current cursor position using:

ActiveDocument.AttachedTemplate.AutoTextEntries("Blah").Insert
Where:=Selection.Range, RichText:=True

However if there is a character directly to the left of the cursor I
need to add a space and if one to the right I need to add a space.

Is there some way to determine this from the current cursor position?

You can adapt this as necessary:

Dim TestChar As String

If Selection.Start > ActiveDocument.Range.Start Then
TestChar = ActiveDocument.Range(Selection.Start - 1,
Selection.Start).Text
If (TestChar <> " ") And (TestChar <> vbCr) Then
Selection.InsertBefore " "
Selection.MoveStart wdCharacter, 1
End If
End If

If Selection.End < ActiveDocument.Range.End - 1 Then
TestChar = ActiveDocument.Range(Selection.End, Selection.End + 1).Text
If (TestChar <> " ") And (TestChar <> vbCr) Then
Selection.InsertAfter " "
Selection.MoveEnd wdCharacter, -1
End If
End If
 
D

Default User

Use something like

Dim r As Range
If Selection.Type = 1 Then
Set r = Selection.Range
r.SetRange Selection.Range.Start - 1, Selection.Start + 1
If Left$(r, 1) <> Chr(32) Then
sInputText = Chr(32) & sInputText
End If
If Right$(r, 1) <> Chr(32) Then
sInputText = sInputText & Chr(32)
End If
Selection.Range = sInputText
End If

Whereby string variable "sInputText" can be substituted by yr AutoText entry

Krgrds,
Perry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top