ThisPaul
ThisPaul

Reputation: 41

how to determine if the selection is inside a textbox or not

A few textbox are created by repeating

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

how to determine if the current cursor (or selection) is inside one of the textbox or not? I want to move the cursor out of the textbox if it is in, and do not move the cursor if it is not in.

Upvotes: 2

Views: 1191

Answers (2)

Dirk Vollmar
Dirk Vollmar

Reputation: 176159

You can determine whether your selection is inside a textbox by checking the StoryType property of the selected range:

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

Upvotes: 3

Rick St Jean
Rick St Jean

Reputation: 34

You will need to get the code from here for getting/setting mouse position: https://support.microsoft.com/en-us/kb/152969

Then write the following

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 

Upvotes: -1

Related Questions