[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to add a textbox near the current mouse position (VBA)?

Posted on 2006-06-01
5
Medium Priority
?
324 Views
Last Modified: 2008-02-26
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.AddTextBox(msoTextOrientationHorizontal, 495#, 360#, 72#, 72#, Selection.Range).Select
     Selection.ShapeRange.Top = ???   -->  I cannot find this value

Can somebody help me, plz?
0
Comment
Question by:y_wally
  • 2
  • 2
5 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 16808651
If you select the shape that you have just created, then you have lost your original selection.
Perhaps this will help. It might need some tweaking.

Sub AddTextBoxNearSelection()
    Dim sh As Shape
    Set sh = ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, 495#, 360#, 72#, 72#, Selection.Range)
    sh.Top = Selection.Information(wdVerticalPositionRelativeToPage)
End Sub
0
 

Author Comment

by:y_wally
ID: 16814032
It does not place the textbox near the last position but at a lower place.
0
 
LVL 9

Expert Comment

by:dmang
ID: 16818677
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(Selection.Characters.Count).Select
    'get the top and left values of the selection
    lngLeft = Selection.Information (wdHorizontalPositionRelativeToPage)
    lngTop = Selection.Information(wdVerticalPositionRelativeToPage)
    'Add a drawing canvas
   'add a drawing canvas at the top and left coordinates
    Set shpCanvas = ActiveDocument.Shapes.AddCanvas _
        (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.AddTextbox _
        (Orientation:=msoTextOrientationHorizontal, _
        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
0
 
LVL 9

Expert Comment

by:dmang
ID: 16818740
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).Select
    Else ' add trailing blanks and a chararcter
        Selection.Text = Selection.Text & "  a"
        Selection.Characters(Selection.Characters.Count).Select
    End If
    lngLeft = Selection.Information(wdHorizontalPositionRelativeToPage)
    lngTop = Selection.Information(wdVerticalPositionRelativeToPage)
   
    'Add a text box to the drawing canvas
    Set shpCanvas = ActiveDocument.Shapes.AddCanvas _
        (Left:=lngLeft, Top:=lngTop, Width:=100, Height:=20)

    Set sh = shpCanvas.CanvasItems.AddTextbox _
        (Orientation:=msoTextOrientationHorizontal, _
        Left:=1, Top:=1, Width:=shpCanvas.Width - 1, Height:=shpCanvas.Height - 1)
    sh.TextFrame.TextRange.Text = "Guess Who?"
   
End Sub
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 300 total points
ID: 16820726
Sorry. I missed a line out.

Sub AddTextBoxNearSelection()
    Dim sh As Shape
    Set sh = ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, 200#, 360#, 72#, 72#, Selection.Range)
    sh.RelativeVerticalPosition = wdRelativeVerticalPositionPage
    sh.Top = Selection.Information(wdVerticalPositionRelativeToPage)
End Sub
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This tutorial provides instructions on how to properly format your Word document using the inbuilt tools provided. The benefits of using these tools means your documents are more accessible and easily portable to other applications an…
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question