-
code for inserting textbox at cursor point
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.
-
Left and Top defined the coordinates from the shape location. All you need to do is determine where the IP is:
[VBA]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[/VBA]
-
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
-
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
-
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!!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules