This code was tested on a form with a CommandButton, Unbound TextBox and
a TextBox bound to a Memo field. You can figure out the names of the
controls from the code:
When you are sending the EM_GETSEL message you are sending the variables
address(ByREF) so that the API can return the Selstart and SelEnd values
back to you.
When you are sending the EM_SETSEL message you are sending the variables
actual values(ByVal) since you are sending not receiving values.
Private Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" _
(ByVal hWnd As Long, _
ByVal wMsg As Long, _
ByRef wParam As Long, _
ByRef lParam As Long) As Long
Private Declare Function GetFocus Lib "user32" () As Long
Private Const EM_GETSEL = &HB0
Private Const EM_SETSEL = &HB1
Private Const EM_GETLINECOUNT = &HBA
Private Const EM_LINEINDEX = &HBB
Private Const EM_LINELENGTH = &HC1
Private Const EM_LINEFROMCHAR = &HC9
Private Sub testmemo_Click()
Dim lCurPos As Long
Dim hWnd As Long
' Start and End of current selection
Dim lStart As Long, lEnd As Long
' The TextBox control MUST HAVE THE FOCUS
Me.testmemo.SetFocus
' Get the hWnd of the Active window
hWnd = GetFocus
'get the character position of the cursor
Call SendMessage(hWnd, EM_GETSEL, lStart, lEnd)
Me.txtSelStart.Value = "Start:" & lStart & vbCrLf & "End:" & lEnd
End Sub
Private Sub cmdSelStart_Click()
On Error GoTo Err_cmdSelStart_Click
Dim lCurPos As Long
Dim hWnd As Long
' Start and End of current selection
Dim lStart As Long, lEnd As Long
' The TextBox control MUST HAVE THE FOCUS
Me.testmemo.SetFocus
' Get the hWnd of the Active window
hWnd = GetFocus
lStart = IIf(CLng(Me.txtSelStart.Value > -1), Me.txtSelStart.Value, 0)
'Set the character position of the cursor
Call SendMessage(hWnd, EM_SETSEL, ByVal lStart, ByVal lStart + 0)
Exit_cmdSelStart_Click:
Exit Sub
Err_cmdSelStart_Click:
MsgBox Err.Description
Resume Exit_cmdSelStart_Click
End Sub
--
HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.
"Oren" <or**@gdblegal.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Stephen -
I'm quite unfamiliar with API declarations. Up until now, I've been
using the selStart property of a textbox to find/set the location of a
cursor in a textbox.
Oren