Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determine value or character to left or right of cursor? 2

Status
Not open for further replies.

Genepoz

Technical User
Aug 7, 2001
38
US
Seems like this should be the easiest thing to find in all those VB for applications books, but I can't find it. I'm used to WP macro (?rightchar) etc. commands, but cannot figure similar function for MS Word macros. Sure would save me a lot of time if someone could answer this (newbie?) question.
Thanks in advance.
 
Use this code:

char = Selection.Characters(1).Text
MsgBox "The first character after cursor is... " & char

Patrick.

PS. You should post VBA questions in the VBA forum.
 
Wolfert,
Thanks. Now how do check what's BEFORE the cursor?

BTW: Oh. I was looking at the left column of this page for the forums and I didn't see those on the right side. Thanks again.
-Gene
 

...in a textbox??


Char = Mid(Text2.Text, Text2.SelStart, 1)
MsgBox "The first character before the cursor is... " & Char
Char = Mid(Text2.Text, Text2.SelStart + 1, 1)
MsgBox "The first character after the cursor is... " & Char

You'll also have to check if the cursor is at the beginning of the text box (SelStart is 0 -invalid procedure error in the Mid() function)




Mark


A molehill man is a pseudo-busy executive who comes to work at 9 AM and finds a molehill on his desk. He has until 5 PM to make this molehill into a mountain. An accomplished molehill man will often have his mountain finished before lunch
- Fred Allen
 
Mark,

Thanks. That could be useful eventually, but I rarely use text boxes in Word. How about to the left of the cursor anywhere in the document? And I don't use forms, just macros.
-Gene

Ha ha. Molehill story funny.
 
Sorry about that. Didn't read the question all the way.

I don't think there is a straight forward method of determining the characters, but you could try a brute force method:


Selection.MoveLeft wdCharacter, Count:=1, Extend:=wdExtend
PriorCursor$ = Selection.Characters(1)
Debug.Print PriorCursor$

Selection.MoveRight wdCharacter, Count:=1, Extend:=wdExtend
AfterCursor$ = Selection.Characters(1)
Debug.Print AfterCursor$




Mark


A molehill man is a pseudo-busy executive who comes to work at 9 AM and finds a molehill on his desk. He has until 5 PM to make this molehill into a mountain. An accomplished molehill man will often have his mountain finished before lunch
- Fred Allen
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top