I found a function online that finds the cursor in a textbox, and then allows me to insert a string where the cursor is. When I call it as the website suggested, it works fine. However, it selects the whole field. I added a part that makes it so that it finds where the cursor was + the length of the added string and sets the .SelStart to this value. However, when I try to use this added ability, it adds a carriage return at my .SelStart point. I can't figure out what is happening.
Here is the original way that I called the function:
Here is the way that adds the carriage return:
Interesting note: If i comment out the line .SelStart = intNextStart, then it wipes out the entire string that was in the textbox (Me.LegalWordin g), inserts the carriage return and does not add the string that I wanted to add.
Here is the entire ReplaceViaPosit ion() function:
I added lines #19 & 20, 54 & 55, and 68 & 69 so that I could then set the cursor at the end of the inserted text.
Here is the link to the website where I found this function: ReplaceViaPosit ion
The function obviously works as it works when just performing its original purpose, so I don't think that the function is the problem (I could be wrong). I think that there is something about the way that I call the function my way that throws it for a loop.
gCursorPosition and gCursorLen are declared as integers.
Here is the original way that I called the function:
Code:
LegalWording = ReplaceViaPosition(Me.LegalWording.Text, gCursorPosition, gCursorLen, "%year%", True)
Code:
With Me.LegalWording
.SetFocus
LegalWording = ReplaceViaPosition(.Text, gCursorPosition, gCursorLen, "%month%", False)
.SelStart = intNextStart
End With
Here is the entire ReplaceViaPosit ion() function:
Code:
Public intNextStart As Integer
' ARGUMENTS:
' *strText: The original text to modify
' *intStart: The start position to enter or replace text (0 based)
' *intLen: The length of the selection to replace (0 based) (default 0)
' *strAdd: The text to add to the string (default ZLS)
' *bPadAddition: True to pad the added text with a space (default False)
' * * * * * * * *If the addition is at the start of the string no leading
' * * * * * * * *space will be added.
' * * * * * * * *If the padding results in two consecutive spaces the
' * * * * * * * *consecutive space will be removed
' * * * * * * * *Padding operations will be bypassed if strAdd is a ZLS
Public Function ReplaceViaPosition( _
ByVal strText As String, _
intStart As Integer, _
Optional intLen As Integer = 0, _
Optional strAdd As String = "", _
Optional bPadAddition As Boolean = False _
) As String
On Error GoTo Error_Proc
Dim Ret As String
'=========================
Dim strOriginal As String
'=========================
'retain the original in case of error
strOriginal = strText
'set the value of where the cursor should be after the inserted text
intNextStart = intStart + Len(strAdd)
'validate the position and length to make sure
'they're within an appropriate range
If ((intStart) > Len(strText)) Or _
(intStart + intLen) > Len(strText) Then
'bad entry, let's raise error 9: Subscript out of range
Err.Raise 9, "ReplaceViaPosition", "Subscript out of range"
End If
'remove any text that is selected due to intLen
If intLen > 0 Then
strText = Left(strText, intStart) & Mid(strText, intStart + intLen + 1)
End If
If Len(strAdd) <> 0 Then
'add strAdd to the string in the specified position
strText = Left(strText, intStart) & strAdd & Mid(strText, intStart + 1)
'perform the padding if required
If bPadAddition Then
'work on the leading space first
'if we're adding to the start of the string we don't need
'any leading spaces
If intStart > 0 Then
'find out if the char before the addition is a space
If Mid(strText, intStart, 1) <> " " Then
'it's not a space, we'll need to enter one
strText = Left(strText, intStart) & " " & Mid(strText, intStart + 1)
'we added a space, so let's increase our intStart by one
'to compensate for the next check
intStart = intStart + 1
'increase intNextStart by 1 to account for the added space
intNextStart = intNextStart + 1
End If
End If
'work on the trailing space next
'find out if the char after the addition is a space
If Mid(strText, intStart + 1 + Len(strAdd), 1) <> " " Then
'it's not a space, we'll need to enter one
'but first check if it's the end of the text...
If (intStart + Len(strAdd)) < Len(strText) Then
'ok, we're safe to add a space after the addition
strText = Left(strText, intStart + Len(strAdd)) & " " & Mid(strText, intStart + Len(strAdd) + 1)
'increase intNextStart by 1 to account for the added space
intNextStart = intNextStart + 1
End If
End If
End If
End If
Ret = strText
'=========================
Exit_Proc:
ReplaceViaPosition = Ret
Exit Function
Error_Proc:
Ret = strOriginal
MsgBox "Error: " & Trim(Str(Err.Number)) & vbCrLf & _
"Desc: " & Err.Description & vbCrLf & vbCrLf & _
"Module: modReplaceViaPosition, Procedure: ReplaceViaPosition" _
, vbCritical, "Error!"
Resume Exit_Proc
Resume
End Function
Here is the link to the website where I found this function: ReplaceViaPosit ion
The function obviously works as it works when just performing its original purpose, so I don't think that the function is the problem (I could be wrong). I think that there is something about the way that I call the function my way that throws it for a loop.
gCursorPosition and gCursorLen are declared as integers.
Comment