SelStart behavior not consistent

WorkingVBA

Registered User.
Local time
Today, 00:36
Joined
Jul 3, 2013
Messages
33
Hi,

I am trying to add a "paste plain text" button in a textbox with rich text enabled to prevent formatted text from being entered. (there may be a better way to do this and if you know it I am all ears)

The idea is that I paste the content of the clip board in the cursor's location which is captured in the Textbox's On_Exit event in a global Integer variable vProjNotesPos:

Private Sub ProjNotes_Exit(Cancel As Integer)
vProjNotesPos = ProjNotes.SelStart​
End Sub​

In the click event of the "Paste as Text" button I set focus to the text box and set the cursor location to the captured position. Then I paste the content of the clip board:

Me.ProjNotes.SetFocus
Me.ProjNotes.SelStart = vProjNotesPos
DoCmd.RunCommand (acCmdPaste)

This works just the first time around. When I click the button a second time it places the cursor at the end of the text string instead of where it is was before I clicked the button a second time. I have looked in a number of places and it seems that somehow the selStart property has to be reset. I tried saving and a number of other things but to no avail. Any ideas? BTW, Merry Christmas
 
Last edited:
Thanks sxschech,
I played around with the suggested code, which I had not seen before. However it deals solely with inserting the text in the textbox and not with retrieving the contents from the clipboard. Using it brings a host of additional problems, for example, you have to call the code using some kind of short cut to be able to capture the active control. Since I am pasting, I would like to use Ctrl + V, but apparently windows does not just allow you to over write an existing short cut like that. (Bummer). So I would like to open this up to another suggestion while I try to hack away at this little challenge at the same time.

Also The code does not insert the text in the right place, and I think I just figured out why (Also part of the original problem) This is a "rich text" textbox which contains all manner of invisible control characters (point of having a rich text textbox) I think those characters are being counted thus not placing the cursor back in the right place. It should when you think about it, but it doesn't

The biggest issue I am having with this is the fact that the .SelStart property returns the end of the string position consistently after it has been initially used. There has to be a way to reset the property without closing the form.
 
Last edited:
Ok, I think I found it:

The problem was in that the textbox On_Exit and On_Lost_Focus events were somehow messing with the results of the .SelStart property.

I solved it by capturing the cursor position (.SelStart) in the Mouse_Up event (after the user clicks in the textbox - when the user releases the mouse click) and the Key_Up event (after the user releases a key as would be the case with typing or using the arrow keys)

The code for trapping the cursor position in a global variable and pasting it in the textbox, using a button named cmdPasteText, is as follows:
Code:
Option Compare Database

Dim vProjNotesPos As Integer

Private Sub ProjNotes_KeyUp(KeyCode As Integer, Shift As Integer)
    vProjNotesPos = Me.ProjNotes.SelStart
End Sub

Private Sub ProjNotes_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    vProjNotesPos = Me.ProjNotes.SelStart
End Sub

Private Sub cmdPasteText_Click()
On Error GoTo cmdPasteText_Click_Err
    Dim vClipboard As MSForms.DataObject
    Set vClipboard = New MSForms.DataObject
    ' Activate Microsoft Forms 2.0 Object Library
    ' Found in c:\windows\system32\FM20.DLL

    ' Return Focus to ProjNotes textbox and set the cursor position
    Me.ProjNotes.SetFocus
    Me.ProjNotes.SelStart = vProjNotesPos
    
    ' Paste the text
    DoCmd.RunCommand (acCmdPaste)
    'Me.ProjNotes.SelStart = 0
    
cmdPasteText_Click_Exit:
    Me.cmdPasteText.Value = 0
    Exit Sub

cmdPasteText_Click_Err:
    MsgBox Error$
    Resume cmdPasteText_Click_Exit
End Sub

Glad I got this figured out and thank you sxschech for your input.

Happy New Year! :)
 

Users who are viewing this thread

Back
Top Bottom