By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,786 Members | 1,131 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,786 IT Pros & Developers. It's quick & easy.

Insert textbox in Word Document VBA

P: 1
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:

Expand|Select|Wrap|Line Numbers
  1.     Declare PtrSafe Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
  2.      'Create custom variable that holds two integers
  3.     Type POINTAPI
  4.         Xcoord As Long
  5.         Ycoord As Long
  6.     End Type
  7.  
  8.     Sub GetCursorPosDemo()
  9.         Dim llCoord As POINTAPI
  10.          'Get the cursor positions
  11.         GetCursorPos llCoord
  12.          'Display the cursor position coordinates
  13.         MsgBox "X Position: " & llCoord.Xcoord & vbNewLine & "Y Position: " & llCoord.Ycoord
  14.     End Sub
  15.  
I even got the following:

Expand|Select|Wrap|Line Numbers
  1. Sub GetPos()
  2. Dim pLeft As Long
  3. Dim pTop As Long
  4. Dim pWidth As Long
  5. Dim pHeight As Long
  6.  
  7. ActiveWindow.GetPoint pLeft, pTop, pWidth, pHeight, _
  8.     Selection.Range
  9. MsgBox "Left = " & pLeft & vbLf _
  10.     & "Top = " & pTop & vbLf _
  11.     & "Width = " & pWidth & vbLf _
  12.     & "Height = " & pHeight
  13. End Sub
  14.  
I have the following code to insert the textbox:

Expand|Select|Wrap|Line Numbers
  1. Sub InsertTB()
  2.     Dim Shp As Shape
  3.  
  4.     Set Shp = ActiveDocument.Shapes.AddTextbox(1, pTop , pLeft , 288, 72)
  5.     With Shp
  6.         .TextFrame.TextRange.InsertSymbol Font:="+Body", CharacterNumber:=9679, Unicode:=True
  7.         .Line.Visible = msoFalse
  8.         .Fill.Visible = msoFalse
  9.         .Width = 36#
  10.         .Height = 36#
  11.     End With
  12.     Set Shp = Nothing
  13. End Sub
  14.  
I just set the box in the beginning of the page
Mar 17 '16 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.