PDA

View Full Version : code for inserting textbox at cursor point



rtinaro
10-10-2012, 05:38 PM
Hello all and thank you in advance for any help.

I've been creating macros to help me make math tests and worksheets (I am a HS math teacher). I am trying to learn the code for inserting a textbox or shape near the cursor in a word document. The macro I have for inserting a text box is

Dim Shp As Shape
Set Shp = ActiveDocument.Shapes.AddTextbox( _
Orientation:=msoTextOrientationHorizontal, _
Left:=120, Top:=120, Width:=288, Height:=72)
Shp.TextFrame.TextRange.InsertSymbol Font:="+Body", CharacterNumber:=9679, Unicode:=True
Shp.line.Visible = msoFalse
Shp.Fill.Visible = msoFalse
Shp.Width = 36#
Shp.Height = 36#
Set Shp = Nothing

I know the AddTextbox(_Orientation...) code determines where the textbox is inserted, and I have tried to find code that would place the text box on the current page or at the cursor. I've had no luck, and I would appreciate any advice.

I have seen some code that uses "InsertAfter," but I couldn't get anything to work. Thanks again for any help.

gmaxey
10-11-2012, 05:22 AM
Left and Top defined the coordinates from the shape location. All you need to do is determine where the IP is:

Sub InsertTB()
Dim Shp As Shape

Set Shp = ActiveDocument.Shapes.AddTextbox(1, fcnXCoord, fcnYCoord, 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
Function fcnXCoord() As Double
fcnXCoord = Selection.Information(wdHorizontalPositionRelativeToPage)
End Function
Function fcnYCoord() As Double
fcnYCoord = Selection.Information(wdVerticalPositionRelativeToPage)
End Function

rtinaro
10-11-2012, 06:25 PM
Thank you Greg. The code you wrote works perfectly and does exactly what I wanted. Awesome, thanks again. I'll make sure to check out your website

Rob

rv.aleixo.al
04-20-2013, 04:45 AM
Hello!

Having a similar task to do, searching in this forum I found this post that helped me with my problem.
I was building a macro for inserting a oval shape in the current selected cell of a table. That is: the cell isn't selected but the cursor is at the cell where I want to insert the shape.
The difficulty was to make the macro recognize the cell. With this bit of code you've put here I managed to do what I wanted. Here it goes for anyone who may need it.

Sub InsTurnoBrancas()
'
'Inserir indicador de turno das brancas
'
On Error GoTo ErrorHandler
Set IndTurnBranc = ActiveDocument.Shapes.AddShape(Type:=msoShapeOval, _
Left:=fcnXCoord, Top:=fcnYCoord, Width:=CentimetersToPoints(0.4), Height:=CentimetersToPoints(0.4))

With IndTurnBranc
.Line.Weight = 0.75
.Line.ForeColor = vbBlack
.Fill.ForeColor = vbWhite
.WrapFormat.Type = wdWrapNone
.LockAnchor = True
.LayoutInCell = True
.RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn
.RelativeVerticalPosition = wdRelativeVerticalPositionParagraph
End With
Exit Sub
ErrorHandler:
End Sub

Function fcnXCoord() As Double
'I've to sum the 8 centimeters to the point where the cursor is.
fcnXCoord = Selection.Information(wdHorizontalPositionRelative ToPage) + CentimetersToPoints(8)
End Function

Function fcnYCoord() As Double
'I've to subtract the 1.5 centimeters to the point where the cursor is because I've a picture in the cell so the cursor is not on the left upper of the cell, it's after the picture.
fcnYCoord = Selection.Information(wdVerticalPositionRelativeTo Page) - CentimetersToPoints(1.5)
End Function

sonjuraec
03-14-2017, 02:43 PM
Just wanna' say hi! Once again- you are helping me with my super-cool toolbar that I created to grade papers for my classes! I'm updating it to work with 2016 and adding some great stuff. You are FANTASTIC!!