如何确定选择是否在文本框内

时间:2016-08-31 06:25:43

标签: vba ms-word textbox word-vba

通过重复

创建一些文本框
Set Shp = ActiveDocument.Shapes.AddTextbox(Orientation:=msoTextOrientationHorizontal, 
          Left:=0, Top:=0, Width:=100, Height:=100)

如何确定当前光标(或选择)是否在其中一个文本框内?我希望将光标移出文本框,如果光标不在,则不要移动光标。

2 个答案:

答案 0 :(得分:1)

您可以通过选中所选范围的StoryType属性来确定您的选择是否在文本框中:

Set Shp = ActiveDocument.Shapes.AddTextbox(Orientation:=msoTextOrientationHorizontal, _
                Left:=0, Top:=0, Width:=100, Height:=100)

If Selection.StoryType = WdStoryType.wdTextFrameStory Then

    ' select the paragraph the shape is anchored to
    Shp.Anchor.Select

    ' collapse to the beginning of the paragraph
    Selection.Collapse
End If

答案 1 :(得分:-1)

您需要从此处获取代码以获取/设置鼠标位置: https://support.microsoft.com/en-us/kb/152969

然后写下面的

Private Sub TextBox1_MouseMove(ByVal intButton As Integer, ByVal intShift As Integer, ByVal sngWidth As Single, ByVal sngHeight As Single) 

    If (sngWidth < 5 Or sngWidth > TextBox1.Width - 5) Or (sngHeight < 5 Or sngHeight > TextBox1.Height - 5) Then 
        SetCursorPos x, y
    End If 
End Sub