选择文本块直到空行,Word VBA

时间:2013-10-24 15:49:25

标签: vba ms-word word-vba

我试图选择一行或多行,直到在文档开头找到空白/空行,这将是标题。使用宏录制器录制的宏使用selection.movedownunit.Not确定必须是什么这里使用的行可以是一行或多行.WDLine,WDParagraph根据计数参数选择(因为行数不是常数)。 使用VBNullString,^ P ^ P,(“\ Blank).empty但是抛出错误。

自动生成的代码是:

Sub SelectTillBlankLine()
    Selection.MoveDown Unit:=WDLine , Count:=4 , Extend:=wdExtend
    Selection.Style = WDStyleTitle

End Sub

感谢任何建议......

1 个答案:

答案 0 :(得分:3)

在我看来,最好的选择是在这种情况下使用.Find object。尝试使用此代码(并查看下面的一些评论):

Sub SelectTillBlankLine()

    'as of current selection we search for anything with two consecutive paragraphs
    'the second one is empty
    Selection.Find.Execute "*^13^13", , , True

    'some correction of range- remove last paragraph from selection
    ActiveDocument.Range(Selection.Start, Selection.End - 1).Select

    'the rest of your code
    Selection.Style = WDStyleTitle

End Sub

尝试并测试以获取简单文档。