单词:有没有一种方法可以将光标首先定位在形状中?

时间:2020-09-21 12:30:58

标签: vb.net ms-word

我在文档中填充了内容(例如文本,表格)的任何点(绝对位置)插入一个形状(在Word中:命名为Word的文本框:Word>插入>展开“文本框”>绘制文本框)与文字,...)。但是现在我想将光标移动到形状的第一个位置。我该怎么办?

我的尝试(我认为注释时,它将光标置于文档的第一位置):

    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

1 个答案:

答案 0 :(得分:1)

我不处理vb.net,但这是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

这会将插入点放置在每种形状中任何文本的开头。

如果您只想插入一个形状并将插入点放在该形状的文本框架的开始处...

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

我希望这会有所帮助。

相关问题