PDA

View Full Version : Solved: How to delete all empty rows in Word?



Erdin? E. Ka
12-12-2006, 05:14 PM
Hi everyone, :hi:

I tried to delete all empty rows in Word (not a table's rows, only page rows) but doing nothing.

I think there is an not-viewable character in end of rows.

I should i do?:think:

Sub DeleteEmptyRowsInWordApplication()
Dim R_o_w
For Each R_o_w In ActiveDocument.Words
R_o_w.Select
If Len(Selection) = 0 Then R_o_w.Delete Unit:=wdCharacter, Count:=1
Next
End Sub

Thanks in advance.

mdmackillop
12-12-2006, 06:21 PM
Can you post a sample of your document?

Erdin? E. Ka
12-12-2006, 07:58 PM
Hi ,

There is a very easy sample file.

I want to delete between 3 and 5 also 8 and 10.

mdmackillop
12-13-2006, 01:31 AM
Hi Erdinc,
Your document seems to contain only extra paragraph marks (pilcrows). This should get rid of them. If it's not that, then you could create code to return all the ascii codes to determine characters present.
Regards
MD

Sub DelPara()
Do
With Selection.Find
.Text = "^p^p"
.Replacement.Text = "^p"
.Forward = True
.Wrap = wdFindContinue
End With
Loop Until Selection.Find.Execute(Replace:=wdReplaceAll) = False
End Sub

fumei
12-13-2006, 01:59 AM
I think there is an not-viewable character in end of rows.

1. There is no such thing, really, a "row" in Word.
2. Yes, there is a non-printable character at the end of each "row". That is because each "row" is a paragraph. Every paragraph has a paragraph mark at the end.

If you want to understand Word, and especially if you want to do any serious VBA with Word, you must understand the paragraph mark. It is, in my opinion BY FAR the most critical element in a Word document.

If you do not have Show/Hide set to Show, I strongly suggest you do so. Most people do not like having Show on, but for myself, I never turn it off.

The paragraph (or pilcrow) is a special thing. It is a single character made up of two ASCII characters. I know of no other example of this. The pilcrow (paragraph mark) is Chr(13) & Chr(7). Chr(7) is a holdover from ancient days. It is BELL. Typewriters used to have a bell chime when the carriage reached the end of its range, and you needed to return it - thus....carriage return.

Thus Chr(13) - carriage return AND Chr(7) - Bell

Again - there are NO rows within text in Word (not really), and turn on Show/Hide. You will see those "non-printable" characters.

OK. Your code. You are setting up an object that is working with words. Not rows. Not paragraphs. Just to demonstrate what is happening, try this. Oh, and this is for the document you posted.Sub EveryWord()
Dim aWord
For Each aWord In ActiveDocument.Words
MsgBox aWord & " " & Asc(aWord)
Next
End SubWhat do you think you get? First of al, what is the ASCII of 1, 2, 3, 5, 6, 7, 8, 10? They are:

1 - 49
2 - 50
3 - 51
5 - 53
6 - 54
7 - 55
8 - 56
10 - 58

If you run the code, these will be the messages displayed.

1 49
13
2 50
13
3 51
13
13
5 53
13
6 54
13
7 55
13
8 56
13
13
10 49
13


Each one of those 13's is a paragraph mark. As an aside, note the Asc(aWord) return for "10". aWord will return "10" as text, but Asc(AWord) only returns the ASCII character of the first character of aWord.

In any case - turn on Show/Hide. And do some reading up on paragraph marks. You will not be able to code deeply in Word until you really have paragraph marks fully understood.

For the record, to delete the paragraphs in your document - AS IT IS NOW....Sub DeleteEmptyParagraphs()
Dim oPara As Word.Paragraph
For Each oPara In ActiveDocument.Paragraphs
If Len(oPara.Range) = 1 Then oPara.Range.Delete
Next
End SubEssentially this goes through each paragraph, and if the length is 1, then the paragraph contains ONLY the paragraph mark (never mind it is two ASCII characters...), and can be deleted.

fumei
12-13-2006, 02:46 AM
Not to bleat on Malcolm's post - he got it in as I was writing mine, so I never saw it - using Selection.Find, and a Replace action is not the most efficient code.

What his does - search for two paragraph marks in a row; if this is found replace the two paragraphs with one. Continue searching.

What mine does - look at the length of each paragraph, if that = 1, delete it.

Again, not trashing Malcolm's route, but using Search is again not as efficient as using numbers.

Finally, while it seems you do not ave Show/Hide as on, I have certainly found that it is common to see "empty" paragraphs...that are not really empty. They have a single space, followed by the paragraph mark. Like this:

text text text text.
[space][paragraph mark]
text text text

Malcom's code does a search for paragraph mark, immediately followed by paragraph mark. In other words, the search would not find this occurence.

I would like to add that the most common reason for having those "empty" paragraphs (with a space or not), is to add visual space between paragraph of text.

This is not needed, and is a poor use of Word. Using Styles totally eliminates this issue. You can have the visual space between paragraph of text built right into the document styles.

In any case, if you DO have that occurence - a space and paragraph - again Malcolm's search will not find it. Here is some code that will, and check to see if the paragraph in question ONLY contains space (regardless of how many), and if there is ONLY spaces, it will delete the paragraph.Sub DeleteEmptyParagraphs()
Dim oPara As Word.Paragraph
Dim var
Dim SpaceCounter As Long
Dim oChar As Word.Characters
For Each oPara In ActiveDocument.Paragraphs
If Len(oPara.Range) = 1 Then
oPara.Range.Delete
Else
SpaceCounter = 0
Set oChar = oPara.Range.Characters
For var = 1 To oChar.Count
If Asc(oChar(var)) = 32 Then
SpaceCounter = SpaceCounter + 1
End If
Next
If SpaceCounter + 1 = Len(oPara.Range) Then
' paragraph contains ONLY spaces
oPara.Range.Delete
End If
End If
Next
End Sub

Erdin? E. Ka
12-13-2006, 10:41 AM
:hi: Hi Malcolm and Gerry,

Your helps and descriptions are very explanatory for me. Now i am tinking about Word-VBA is a more complicated | detailed applicaiton. At the first time i supposed it isn't difficult matter, but now i understood it is not like that. But i learned very important knowledges, thank you very much to kindly helps.:yes

Best regards.:friends:

mdmackillop
12-13-2006, 11:27 AM
I totally agree with Gerry, but the thing about my simple code is that you can very easily modify it to get rid of many forms of unwanted fromatting in a poorly presented document. EG Replace ^t^t with^t will clear all those nasty tabs, " " with " " for unwanted spaces and so on. A little practice and you can very quickly transform rubbish into a useful document, especially when you get to grip with Styles.

fumei
12-13-2006, 12:18 PM
Yes! Malcolm is very correct in this. Using Search and Replace is very helpful when you get a handle on the items like ^t for Tabs, ^p for paragraphs, etc.

There ARE some limitations on what you can put in as Replace though. Replace will not allow some things.

What it really comes down to is: if you are doing stuff "manually", that is, using menu actions like Replace, then getting to know Find and Replace well is a GOOD thing. Absolutely, you can do some rapid fixing with it.

If you are doing things by code (VBA), then it is much better (as much as possible) to use objects, and the object model. Things like Paragraph, Characters, Ranges etc etc.; logic statements such as For Each, Select Case etc.

Certainly as a starting place, using the Find and Replace dialog is the way to go. Be sure to look at the Format and Special buttons, and see what they can add to your work.

ryanadanders
05-01-2014, 07:03 AM
Not to bleat on Malcolm's post - he got it in as I was writing mine, so I never saw it - using Selection.Find, and a Replace action is not the most efficient code.

What his does - search for two paragraph marks in a row; if this is found replace the two paragraphs with one. Continue searching.

What mine does - look at the length of each paragraph, if that = 1, delete it.

Again, not trashing Malcolm's route, but using Search is again not as efficient as using numbers.

Finally, while it seems you do not ave Show/Hide as on, I have certainly found that it is common to see "empty" paragraphs...that are not really empty. They have a single space, followed by the paragraph mark. Like this:

text text text text.
[space][paragraph mark]
text text text

Malcom's code does a search for paragraph mark, immediately followed by paragraph mark. In other words, the search would not find this occurence.

I would like to add that the most common reason for having those "empty" paragraphs (with a space or not), is to add visual space between paragraph of text.

This is not needed, and is a poor use of Word. Using Styles totally eliminates this issue. You can have the visual space between paragraph of text built right into the document styles.

In any case, if you DO have that occurence - a space and paragraph - again Malcolm's search will not find it. Here is some code that will, and check to see if the paragraph in question ONLY contains space (regardless of how many), and if there is ONLY spaces, it will delete the paragraph.Sub DeleteEmptyParagraphs()
Dim oPara As Word.Paragraph
Dim var
Dim SpaceCounter As Long
Dim oChar As Word.Characters
For Each oPara In ActiveDocument.Paragraphs
If Len(oPara.Range) = 1 Then
oPara.Range.Delete
Else
SpaceCounter = 0
Set oChar = oPara.Range.Characters
For var = 1 To oChar.Count
If Asc(oChar(var)) = 32 Then
SpaceCounter = SpaceCounter + 1
End If
Next
If SpaceCounter + 1 = Len(oPara.Range) Then
' paragraph contains ONLY spaces
oPara.Range.Delete
End If
End If
Next
End Sub


I know it's been many years but what if tabs were a possibility as well. For instance a line that only contained tabs, spaces, and the paragraph mark.

So, I have an idea how to do this one as well...but the orriginal code isn't working and I don't know exactly why.

The error is "Run-time error'5': Invalid procedure call or argument" and it highlights "If Asc(oChar(var)) = 32 Then"

Any idea why?

*Note: I submitted this as a question at this link:
stackedoverflow(dot)com/questions/23411007/call-argument-error-with-asc-delete-unnecessary-lines-in-msword-2007

macropod
05-01-2014, 03:39 PM
You could probably do the entire clean-up with a pair of wildcard Find/Replace sequences:
Find = [^t^l^0160 ]{1,}^13{1,}
Replace = ^p
Find = [^13]{2,}
Replace = ^p
It all depends on what characters are in the unwanted 'empty' lines. The above is more comprehensive than the above macros (though it could be implemented as a macro), as it deletes all lines that consist of ordinary spaces, non-breaking spaces, tabs & manual line breaks followed by a paragraph break, plus any empty paragraphs. The above macros only deal with empty paragraphs and (in fumei's case) paragraphs that contain just normal spaces.