Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

remove blank lines in Word Doc 1

Status
Not open for further replies.

CoSpringsGuy

IS-IT--Management
Aug 9, 2007
955
US
Just learning VBA .....

I need a snippet of code that will remove blank lines from a word doc. Im working it but am on a time crunch if anyone has anything that might help.

_____________________________________
Crystal Reports XI Developer Version
Intersystems Cache 5.X ODBC connection

 
No need for VBA.

Search for <CR><CR> and replace with <CR>

Repeat until no replacements are made.
 
If what you mean by "blank lines" is empty paragraphs, then:
Code:
Dim oPara As Paragraph
For Each oPara In ActiveDocument.Paragraphs
   If Len(oPara.Range.Text) = 1 Then
      oPara.Range.Delete
   End If
Next
An "empty" paragraph - just a paragraph mark - has a Len = 1.

But be careful in your terms. The above works if your "blank lines" are, in fact, paragraphs. If the "line" is terminated by a manual line break, then the next "line" is still the SAME paragraph. Thus, that "blank line" would remain.

Do you have Show/Hide on? If not, turn it on. Paragraph marks are pilcrows, manual line breaks are wee arrows pointing to the left.

Gerry
 
Fumei,

Thanks for the explanation! You are indeed correct. I always keep show all codes on and it is in fact the paragraph mark.

So I used your code. However it appeared nothing was happening... After using Debug.Print Len(oPara.Range.Text) & oPara.Range.Text I found in the immediate window that those paragraph mark lines were all a length of 2

I verified again that there were no spaces on the line before and that the preceeding line also ended with the same paragraph mark. Apparently paragraph mark has a length of 2?

Regardless, I changed your code to 2 instead of 1 and got the results I needed..

Thanks so much!

_____________________________________
Crystal Reports XI Developer Version
Intersystems Cache 5.X ODBC connection

 
This is a little tricky, and I am glad you reworked it to function for you.

The paragraph mark in the document (at least in 2002 which I use) has a len = 1. For example:
Code:
Option Explicit

Sub EmptyPara()
Dim oPara As Paragraph
Dim ThisDoc As Document
Dim WriteToDoc As Document

Set ThisDoc = ActiveDocument
Set WriteToDoc = Documents.Add

For Each oPara In ThisDoc.Paragraphs
   Selection.TypeText Len(oPara.Range.Text) & " " & _
      oPara.Range.Text
Next
End Sub
This writes the Len of each paragraph, plus a space, plus the actual text, into a new document.

If the source doc is:

<p>
Y<p>
<p>
Yadda<p>
<p>
Blah blah<p>
Eee<p>
<p>
<p>

With <p> being a paragraph mark, there are five "empty" paragraphs. The result in the new document is:

1
2 Y
1
6 Yadda
1
10 Blah blah
4 Eee
1
1

As you can see, the Len of the "empty" paragraph is quite definitely = 1.

<p> 1
Y<p> 2 - "Y" and <p>
<p> 1
Yadda<p> 6 - "Yadda" and <p>

What version are you using?

Gerry
 
Word 2003

thanks for your explanation on the subject!

_____________________________________
Crystal Reports XI Developer Version
Intersystems Cache 5.X ODBC connection

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top