Insert textbox in Word Document VBA

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Thinus
    New Member
    • Mar 2016
    • 1

    Insert textbox in Word Document VBA

    I need to insert a textbox at the position of the mouse pointer in a Page of a word document whether there are text or not.

    I have the following code to get the position:

    Code:
        Declare PtrSafe Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
         'Create custom variable that holds two integers
        Type POINTAPI
            Xcoord As Long
            Ycoord As Long
        End Type
    
        Sub GetCursorPosDemo()
            Dim llCoord As POINTAPI
             'Get the cursor positions
            GetCursorPos llCoord
             'Display the cursor position coordinates
            MsgBox "X Position: " & llCoord.Xcoord & vbNewLine & "Y Position: " & llCoord.Ycoord
        End Sub
    I even got the following:

    Code:
    Sub GetPos()
    Dim pLeft As Long
    Dim pTop As Long
    Dim pWidth As Long
    Dim pHeight As Long
    
    ActiveWindow.GetPoint pLeft, pTop, pWidth, pHeight, _
        Selection.Range
    MsgBox "Left = " & pLeft & vbLf _
        & "Top = " & pTop & vbLf _
        & "Width = " & pWidth & vbLf _
        & "Height = " & pHeight
    End Sub
    I have the following code to insert the textbox:

    Code:
    Sub InsertTB()
        Dim Shp As Shape
    
        Set Shp = ActiveDocument.Shapes.AddTextbox(1, pTop , pLeft , 288, 72)
        With Shp
            .TextFrame.TextRange.InsertSymbol Font:="+Body", CharacterNumber:=9679, Unicode:=True
            .Line.Visible = msoFalse
            .Fill.Visible = msoFalse
            .Width = 36#
            .Height = 36#
        End With
        Set Shp = Nothing
    End Sub
    I just set the box in the beginning of the page
Working...