Nandu
Nandu

Reputation: 3

Selecting block of text until blank line, Word VBA

Am trying to select one or more lines until blank/ empty line is found at the start of the document, which would be the title.Did use the macro recorder the recorded macro uses selection.movedownunit.Not sure what has to be used here as the line could be one or more.WDLine,WDParagraph selects depending on the count parameter(as the number of lines is not constant). Did use VBNullString,^P^P,("\Blank).empty but throws a error.

The code auto generated is :

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

End Sub

Appreciate any suggestions...

Upvotes: 0

Views: 3067

Answers (1)

Kazimierz Jawor
Kazimierz Jawor

Reputation: 19067

In my opinion best option is to use .Find object in this situation. Try with this code (and check some comments below):

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

Tried and tested for simple document.

Upvotes: 3

Related Questions