使用多行文本框时,在光标位置插入字符串会使其不正确

时间:2015-01-23 09:16:57

标签: string insert textbox multiline cursor-position

我正在创建一个迷你基本的电子邮件"使用VBA。用户可以选择插入"名称"字段#name#单击多行文本框(即电子邮件正文)中光标位置的按钮。用户点击发送后,代码会将#name#字段替换为与从列表框中选择的相应地址关联的名称。

为文本框启用了Tab和enter。 这里的问题是,当用户按下回车键时,Len()函数将其作为3个字符选中?例如:

用户输入:

A

C:|

Len()将字符串长度选为9个字符。因此,假设用户想要在"之后插入名称字段:"在上面指示的光标位置,它放置如下:

A

'#名称#C:|

(没有')光标位置正确移动并保持在":"

之后

以下是我正在使用的代码(大部分来自其他来源):

'Insert String at cursor
Sub InsertAtCursor(ControlName As String, InsertString As String)
Dim Ctrl As Object 'Object ref var
Dim strPrior As String 'This var records the string before the cursor
Dim strAfter As String 'This var records the string after the cursor
Dim lngLen As Integer 'Saves the length (number of chars) currently in the text box
Dim iSelStart As Integer 'Saves the current cursor position in the text box

    'Set Ctrl as ref to the MessageBody text box
    Set Ctrl = AdOp.Controls(ControlName)

    With Ctrl
        If .Enabled And Not .Locked Then                
            lngLen = Len(.Text)
            'SelStart can't cope with more than 32k characters.
            If lngLen <= 32767& - Len(InsertString) Then
                'Remember characters before cursor.
                iSelStart = .SelStart
                If iSelStart > 1 Then
                    'Saves all text left of cursor
                    strPrior = Left$(.Text, iSelStart)

                End If
                'Remember characters after selection.
                If iSelStart + .SelLength < lngLen Then
                    strAfter = Mid$(.Text, iSelStart + .SelLength + 1)
                End If
                'Assign prior characters, new string, and later ones.
                .Value = strPrior & InsertString & strAfter
                'Put the cursor back where it was, after the new string.
                .SelStart = iSelStart + Len(InsertString)
                'Return True on success
                'InsertAtCursor = True
            End If
        End If
    End With

End Sub

这里的任何帮助都会很棒。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

同样的问题。 它是由控件内容中存在的CR + LF行尾引起的 - &gt; 2个字符。但是.SelStart每行只计算一个。 因此,应该审查这个例程的完整逻辑,以便解决这个问题。

这是一个似乎有用的(应该​​改进和清除): 我的想法是使用vbLf行分隔符(一个字符)来处理复制缓冲区。

&#39;目的:在活动控件中的光标处插入字符。 &#39;返回:如果插入了字符,则返回True。 &#39;参数:strChars =要在光标处插入的字符。 &#39; strErrMsg =要附加任何错误消息的字符串。 &#39;注意:控件必须具有焦点。 &#39;已调试:将CR + LF(2个字符)上的指针问题的WorkArround作为行分隔符 公共函数FormInsertAtCursor(Frm As UserForm,strChars As String,optional strErrMsg As String)As Boolean On Error GoTo Err_Handler     Dim strPrior As String&#39;光标前的文本。     Dim strAfter As String&#39;光标后的文本。     Dim lngLen As Long&#39;字符数     Dim iSelStart As Integer&#39; Where the cursor is。

If strChars <> vbNullString Then
    With Frm.ActiveControl
        If .Enabled And Not .Locked Then

            Dim myText As String: myText = Replace(Frm.ActiveControl.Text, vbCr, "")
            lngLen = Len(myText)

            'SelStart can't cope with more than 32k characters.
            If lngLen <= 32767& - Len(strChars) Then

                iSelStart = .SelStart

                myText = Left(myText, iSelStart) & strChars & Mid(myText, iSelStart + 1)

                Frm.ActiveControl = myText

                .SelStart = iSelStart + Len(strChars)

                FormInsertAtCursor = True
                Frm.ActiveControl.SetFocus
            End If
        End If
    End With
End If

Exit_Handler:     退出功能

Err_Handler: Debug.Print Err.Number,Err.Description     选择Case Err.Number     案例438&amp;,2135&amp;,2144&amp; &#39;对象不支持此属性。财产是只读的。数据类型错误。         strErrMsg = strErrMsg&amp; &#34;你不能在这里插入文字。&#34; &安培; vbCrLf     案例2474&amp;,2185&amp; &#39;没有主动控制。控制没有焦点。         strErrMsg = strErrMsg&amp; &#34;无法确定将字符插入哪个控件。&#34; &安培; vbCrLf     Case Else         strErrMsg = strErrMsg&amp; &#34;错误&#34; &安培; Err.Number&amp; &#34;:&#34; &安培; Err.Description&amp; vbCrLf     结束选择     恢复Exit_Handler 结束功能

相关问题