user7205862
user7205862

Reputation:

Word: Is there a way to position the cursor first in a shape?

I insert a shape (named text box in Word: Word > Insert > expand the "Text Box" > Draw Text Box) at any point (absolute position) in the document, which is filled with content (e.g. text, table with text, ...). But now I want to move the cursor to the first position in the shape. How do I do that?

My attempt (in my opinion it puts the cursor in the first position in the document when commenting out):

    Dim objWord As Application
    objWord = GetObject(, "Word.Application")
    For i As Integer = 1 To objWord.ActiveDocument.Shapes.Count
        objWord.ActiveDocument.Shapes(i).TextFrame.TextRange.Select() 'select the whole textbox

        'Dim objRange As Range = objWord.ActiveDocument.Range
        'objRange.SetRange(objWord.ActiveDocument.Shapes(i).TextFrame.TextRange.Start, objWord.ActiveDocument.Shapes(i).TextFrame.TextRange.Start) 'position cursor is out of the shape
        'objRange.Select()
    Next

Upvotes: 0

Views: 381

Answers (1)

Charles Kenyon
Charles Kenyon

Reputation: 1026

I do not handle vb.net, but here is Word vba:

Sub SelectTextInShape()
    Dim objWord As Application
    Set objWord = Word
    Dim i As Long
    For i = 1 To objWord.ActiveDocument.Shapes.Count
         objWord.ActiveDocument.Shapes(i).TextFrame.TextRange.Select
        Selection.Collapse wdCollapseStart
        ' Do something here?
    Next i
End Sub

This places the insertion point at the beginning of any text in each shape.

If you merely want to insert a shape and have the insertion point at the beginnig of the textframe for that shape...

Dim objWord As Application
Set objWord = Word
Dim i As Long
Let i = objWord.ActiveDocument.Shapes.Count
objWord.ActiveDocument.Shapes(i).TextFrame.TextRange.Select
Selection.Collapse wdCollapseStart

I hope this helps.

Upvotes: 0

Related Questions