经常在编写word vba 解决方案时会遇到需要获取range对象所在的行的文本内容。
这时候可以使用如下的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
Sub QQ1722187970() Const wdLine = 5 Dim oDoc As Document Dim oRng As Range Dim oWord As Word.Application Set oDoc = Word.ActiveDocument Set oRng = oDoc.Range(1, 100) oRng.Select With oWord.Selection '取消选择区域 .Collapse '定位到行的开头 .HomeKey wdLine iStart = .Start '定位到行的结尾 .EndKey wdLine iEnd = .End End With '返回所在行的Range对象 Set oRng = oDoc.Range(iStart, iEnd) '获取行的内容 sText = oRng.Text End Sub |
原理是使用Sel[……]
阅读全文>>>