y_wally
asked on
How to add a textbox near the current mouse position (VBA)?
Hi,
I have a word document with two parts (left and right).
In the left part, I will write down some text and when I click on a button, then in the right part a textbox should appear on the same height as mij last typed text.
I can insert a textbox, but how can I define the top position?
ActiveDocument.Shapes.AddT extBox(mso TextOrient ationHoriz ontal, 495#, 360#, 72#, 72#, Selection.Range).Select
Selection.ShapeRange.Top = ??? --> I cannot find this value
Can somebody help me, plz?
I have a word document with two parts (left and right).
In the left part, I will write down some text and when I click on a button, then in the right part a textbox should appear on the same height as mij last typed text.
I can insert a textbox, but how can I define the top position?
ActiveDocument.Shapes.AddT
Selection.ShapeRange.Top = ??? --> I cannot find this value
Can somebody help me, plz?
ASKER
It does not place the textbox near the last position but at a lower place.
Sub NewCanvasTextbox()
Dim docNew As Document
Dim shpCanvas As Shape
Dim sh As Shape
Dim lngTop As Long
Dim lngLeft As Long
'add 2 spaces and a character to the selected text
Selection.Text = Selection.Text & " a"
'select the last character of the selection (leaving the original text and 2 spaces
Selection.Characters(Selec tion.Chara cters.Coun t).Select
'get the top and left values of the selection
lngLeft = Selection.Information (wdHorizontalPositionRelat iveToPage)
lngTop = Selection.Information(wdVe rticalPosi tionRelati veToPage)
'Add a drawing canvas
'add a drawing canvas at the top and left coordinates
Set shpCanvas = ActiveDocument.Shapes.AddC anvas _
(Left:=lngLeft, Top:=lngTop, Width:=100, Height:=20)
'Add a text box to the drawing canvas
'add a atext box to the drawing canvas
Set sh = shpCanvas.CanvasItems.AddT extbox _
(Orientation:=msoTextOrien tationHori zontal, _
Left:=1, Top:=1, Width:=shpCanvas.Width - 1, Height:=shpCanvas.Height - 1)
When you select text, make sure that you don't include the line break character.
End Sub
Dim docNew As Document
Dim shpCanvas As Shape
Dim sh As Shape
Dim lngTop As Long
Dim lngLeft As Long
'add 2 spaces and a character to the selected text
Selection.Text = Selection.Text & " a"
'select the last character of the selection (leaving the original text and 2 spaces
Selection.Characters(Selec
'get the top and left values of the selection
lngLeft = Selection.Information (wdHorizontalPositionRelat
lngTop = Selection.Information(wdVe
'Add a drawing canvas
'add a drawing canvas at the top and left coordinates
Set shpCanvas = ActiveDocument.Shapes.AddC
(Left:=lngLeft, Top:=lngTop, Width:=100, Height:=20)
'Add a text box to the drawing canvas
'add a atext box to the drawing canvas
Set sh = shpCanvas.CanvasItems.AddT
(Orientation:=msoTextOrien
Left:=1, Top:=1, Width:=shpCanvas.Width - 1, Height:=shpCanvas.Height - 1)
When you select text, make sure that you don't include the line break character.
End Sub
Me again...
I tried the previous routine after randomly positioning the cursor (i.e. a new line and few tabs) and the textbox appeared on the next line.
It occurred to me that the tb was being positioned on a line break...hence the slight change below...
Sub NewCanvasTextbox()
Dim docNew As Document
Dim shpCanvas As Shape
Dim sh As Shape
Dim lngTop As Long
Dim lngLeft As Long
'line break...add a leading blank
If Asc(Selection.Text) = 13 Then
Selection.Text = " " & Selection.Text
Selection.Characters(1).Se lect
Else ' add trailing blanks and a chararcter
Selection.Text = Selection.Text & " a"
Selection.Characters(Selec tion.Chara cters.Coun t).Select
End If
lngLeft = Selection.Information(wdHo rizontalPo sitionRela tiveToPage )
lngTop = Selection.Information(wdVe rticalPosi tionRelati veToPage)
'Add a text box to the drawing canvas
Set shpCanvas = ActiveDocument.Shapes.AddC anvas _
(Left:=lngLeft, Top:=lngTop, Width:=100, Height:=20)
Set sh = shpCanvas.CanvasItems.AddT extbox _
(Orientation:=msoTextOrien tationHori zontal, _
Left:=1, Top:=1, Width:=shpCanvas.Width - 1, Height:=shpCanvas.Height - 1)
sh.TextFrame.TextRange.Tex t = "Guess Who?"
End Sub
I tried the previous routine after randomly positioning the cursor (i.e. a new line and few tabs) and the textbox appeared on the next line.
It occurred to me that the tb was being positioned on a line break...hence the slight change below...
Sub NewCanvasTextbox()
Dim docNew As Document
Dim shpCanvas As Shape
Dim sh As Shape
Dim lngTop As Long
Dim lngLeft As Long
'line break...add a leading blank
If Asc(Selection.Text) = 13 Then
Selection.Text = " " & Selection.Text
Selection.Characters(1).Se
Else ' add trailing blanks and a chararcter
Selection.Text = Selection.Text & " a"
Selection.Characters(Selec
End If
lngLeft = Selection.Information(wdHo
lngTop = Selection.Information(wdVe
'Add a text box to the drawing canvas
Set shpCanvas = ActiveDocument.Shapes.AddC
(Left:=lngLeft, Top:=lngTop, Width:=100, Height:=20)
Set sh = shpCanvas.CanvasItems.AddT
(Orientation:=msoTextOrien
Left:=1, Top:=1, Width:=shpCanvas.Width - 1, Height:=shpCanvas.Height - 1)
sh.TextFrame.TextRange.Tex
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Perhaps this will help. It might need some tweaking.
Sub AddTextBoxNearSelection()
Dim sh As Shape
Set sh = ActiveDocument.Shapes.AddT
sh.Top = Selection.Information(wdVe
End Sub