vbams-word

Getting a line from a Word document using the Selection object


How can I get a line from a Word document using the Selection object in VBA? Something like this:

Selection.MoveDown Unit:=wdLine, Count:=15
'print the 15th line here

EDIT: When i do:

Selection.MoveDown Unit:=wdLine, Count:=15
MsgBox (Selection.Text)

It prints only the first character of the line.


Solution

  • You need to expand the selection:

    Selection.MoveDown Unit:=wdLine, Count:=15
    Selection.Expand wdLine
    MsgBox (Selection.Text)