Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / December 2004

Tip: Looking for answers? Try searching our database.

Determine if line is blank using ranges

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
D - 02 Dec 2004 14:47 GMT
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
Peter - 02 Dec 2004 16:49 GMT
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

> 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
[quoted text clipped - 17 lines]
>         End With
>     Loop
Greg - 02 Dec 2004 19:53 GMT
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.

 

> Something like the following should serve your purposes:
>
[quoted text clipped - 36 lines]
> >         End With
> >     Loop
D - 02 Dec 2004 20:05 GMT
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.

> Peter,
>
[quoted text clipped - 53 lines]
> > >         End With
> > >     Loop

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.