-
I need to be able to click or doubleclick anywhere in the body of a richtext box and have the value of a string I be inserted into that part of the text file in the richtextbox overwriting what was already there.
For example the first line of the RTFBox is
The cat sat on the mat
I have "dog" in memory as a string variable somewhere when I click just before the c of cat the sentance comes out like this
The dog sat on the mat
and not....
The dogcat sat on the mat.
If you know what I mean.
Anyone any ideas ? Cheers.
DQ
-
Try this.
Code:
Function InsertWord(sIn As RichTextBox, sWord As String)
sIn.SetFocus
For I = 1 To Len(sWord)
sIn.SelText = Mid(sWord, I, 1)
SendKeys "{DEL}"
Next I
End Function
You would use it like:
Code:
InsertWord RichTextBox1, "dog"
-
Or you could do this:
Code:
Function InsertWord(sIn As RichTextBox, sWord As String)
sIn.SetFocus
sIn.SelLength = Len(sWord)
sIn.SelText = Mid(sWord, I, 1)
End Function
Good luck!
-
Thanks guys,
Unfortunately neither of those work for me, Megatron when I put your code in, and then call the function in the on click event of the richtextbox you briefly see "dog" appear displacing the text before it then deletes itself as well as the first digit to the right of where you clicked ie:
The cat sat on the mat ......becomes
The at sat on the mat instead of the dog sat on the mat.
Joacim, firstly I got i not declared as a variable (this project uses Option Explicit), then when I did declare it in the function as an integer I get a run time error 5 "Invalid procedure or argument" on the last line of code.
I am sure that I must be doing something wrong.
DQ
-
This works:
Code:
Private Sub RichTextBox1_Click()
Dim MyString As String, TempString As String
'********************************************
MyString = "dog"
With RichTextBox1
TempString = .Text
Mid(TempString, .SelStart + 1, Len(MyString)) = MyString
.Text = TempString
End With
End Sub
-
I'm sorry my fault. I was coping Megatrons code and must have been in a bit of a hurry. This is what the code should look like:
Code:
Function InsertWord(sIn As RichTextBox, sWord As String)
sIn.SetFocus
sIn.SelLength = Len(sWord)
sIn.SelText = sWord
End Function
Good luck!
-
Indeed it does. Thanks a lot.
-
Sorry to bother you guys again. All of the code works now with one exception. When I have my String Value and insert it into the richtext box, it works fine except when I put it in at the end of a line. In which case it pops the next line up onto the same line as itself and messes the formatting of the file. Like this
'Original file
The cat sat on the mat
The dog sat on the mat
'We put $dog1 at the end of the first line, we want to see..
The cat sat on the mat$dog1
The dog sat on the mat
'What we actually see is.......
The cat sat on the mat£dog1 The dog sat on the mat
Is there any way around this ?
-
Yes, the problem is that you are overwriting the carriage return at the end of the line.
Code:
Function InsertWord(sIn As RichTextBox, sWord As String)
sIn.SetFocus
sIn.SelLength = Len(sWord)
If Right$(sIn.SelText, 1) = vbcrlf then
sIn.SelText = sWord & vbCrLf
Else
sIn.SelText = sWord
End If
End Function
-
shub,
That didn't work. It's doing the exact same thing.
????
-
vbCrLf is two characters change the code to this:
Code:
Function InsertWord(sIn As RichTextBox, sWord As String)
sIn.SetFocus
sIn.SelLength = Len(sWord)
If Instr(sIn.SelText, vbCrLf) Then
sIn.SelText = sWord & vbCrLf
Else
sIn.SelText = sWord
End If
End Function
-
Better
Thanks, Its a bit better, but it still moves the next line back 3 characters
eg:
The cat sat on the mat$dog1
The dog sat on the mat
Do you know why this might be ?
Thanks for your help.
-
Actually that example I just doesn't appear to have come out right , but you get what I mean, the whole second line would be shifted 3 spaces left.
-
Ok try this then:
Code:
Function InsertWord(sIn As RichTextBox, sWord As String)
Dim iPos As Integer
sIn.SetFocus
sIn.SelLength = Len(sWord)
iPos = Instr(sIn.SelText, vbCrLf)
If iPos Then
sIn.SelLength = iPos - 1
sIn.SelText = sWord & vbCrLf
Else
sIn.SelText = sWord
End If
End Function
-
Close
Thats better all right, only one problem remains now, and that is that if I insert the text at the end, the next line keeps its formatting but the carraige return moves it on a line so you get :
The cat sat on the mat$dog1
The dog sat on the mat
Instead of:
The cat sat on the mat$dog1
The dog sat on the mat
I am starting to wonder, wether it is possible to do what it is I want to achieve.
Thanks for your help though.
-
OK lets try this:
Code:
Function InsertWord(sIn As RichTextBox, sWord As String)
Dim iPos As Integer
sIn.SetFocus
sIn.SelLength = Len(sWord)
iPos = Instr(sIn.SelText, vbCrLf)
If iPos Then
sIn.SelLength = iPos - 1
sIn.SelText = sWord 'dont add the vbCrLf
Else
sIn.SelText = sWord
End If
End Function
Now this should work!!!
-
Thanks
Thanks Joacim, you are a star. It works great.