Determine if line is blank using ranges

D

D

I have a procedure that I would like to run in a non-visible instance of
Word. To do this, I believe you cannot use the selection object at all, but
instead have to use ranges. So, I've converted most of the procedure to use
ranges, but there is one part that I'm having trouble with. I use this to
check if a line is blank, if it is, then delete said line, continuing this
check until a non-blank line is found. Anyone have any ideas for how to to
convert this to using ranges? Thanks.

Do
With Selection
.MoveUp unit:=wdLine, Count:=1
.EndKey unit:=wdLine, Extend:=False
.HomeKey unit:=wdLine, Extend:=True
If .Type = wdSelectionIP Then
.Delete
Else
.Collapse direction:=wdCollapseStart
Exit Do
End If
End With
Loop
 
P

Peter

Something like the following should serve your purposes:

Dim para As Paragraph
For Each para In ActiveDocument.Paragraphs
With para.Range
' one thing to remember about a paragraph is that
' it includes the ending paragraph mark as a word
If .Words.Count = 1 Then
para.Range.Delete
Else
Exit For
End If
End With
Next para

hth,

-Peter
 
G

Greg

Peter,

Armed with recently gained knowledge of the Paragraphs collection I toyed
with a similiar solution:

If para.Range = vbCr Then para.Range.Delete

This worked to delete empty lines until I was confronted with an empty line
consisting of a manual line break Chr(11) which appears to be incorporated as
part of the paragraph range. So "Blah, Blah ... line break, line break,
Blah, Blah" would leave an empty line in the text.

I know this can be done with find and replace, but I couldn't find a way to
do it using a range.
 
D

D

The key is that I don't want to delete every blank line, just the ones
between my starting point and the first non-blank line above that starting
point, so here is what I ended up with:

dim rngFind as range
With rngFind
.Collapse direction:=wdCollapseStart
Do
.Move unit:=wdParagraph, Count:=-1
If .Paragraphs(1).Range.Words.Count = 1 Then
.Delete
Else
Exit Do
End If
Loop
End With

Thanks for the help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top