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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove Blank Paragraph at Start/End of Table Cell? 3

Status
Not open for further replies.

clhare

Technical User
May 29, 2003
118
US
The macro I am trying to write is similar to my previous thread "Remove Blank Paragraph before Next Page Section Break?"

This time, I am trying to remove blank paragraphs at the start and at the end of table cells. I used the final macro from my prevous thread as the starting point and came up with the following code. I don't get any errors, but the paragraph marks aren't being deleted either.

Code:
Dim intTableCount As Integer
Dim intTable As Integer
Dim currRow As Row
Dim currCell As Cell
Dim intCharCount As Long
Dim intChar As Long

' Count number of tables in the document
intTableCount = ActiveDocument.Tables.Count

' In each table:
For intTable = 1 To intTableCount
    ' Check each cell in each row of the table
    For Each currRow In ActiveDocument.Tables(intTable)
        For Each currCell In currRow.Cells
            ' Count number of characters in the cell
            intCharCount = .Range.Characters(intChar).Count
            
            ' STEP 1: Starting with the FIRST character in the cell,
            ' check each character to see if it is a paragraph mark
            For intChar = 1 To intCharCount Step 1
                ' If the character is a paragraph mark, need to delete it
                If .Range.Characters(intChar) = vbCr Then
                    .Range.Characters(intChar).Delete
                ' If the character is not a paragraph mark, break out of
                ' the loop and check the next cell
                ElseIf .Range.Characters(intChar) <> vbCr Then
                    Exit For
                End If
            Next
            
            ' STEP 2: Starting with the LAST character in the cell,
            ' check each character to see if it is a paragraph mark
            For intChar = intCharCount To 1 Step -1
                ' If the character is a paragraph mark, need to delete it
                If .Range.Characters(intChar) = vbCr Then
                    .Range.Characters(intChar).Delete
                ' If the character is not a paragraph mark, break out of
                ' the loop and check the next cell
                ElseIf .Range.Characters(intChar) <> vbCr Then
                    Exit For
                End If
            Next
    Next currRow
Next intTable

Any help you can give is greatly appreciated!

Cheryl
 
Hi Cheryl,

You do have a lot of spare paragraph marks! I used to program in Cobol where full stops were critically important elements of the language and very easy to make mistakes with. One chap I worked with used to insist there were a finite number of full stops in the universe and if you took one out of your code somewhere it just settled back somewhere else in another bit of code.

Anyway, your code here.

First obvious problem is several references to [blue].Range[/blue] which are not inside a With Block. They are all related to the Cell and should be qualified with [blue]currCell[/blue] - either individually or by putting a [blue]With currCell[/blue] Block just inside the [purple]For currCell[/purple] Block.

Secondly, you are missing a [blue]Next curCell[/blue] before the Next currRow.

Next, the Row For .. Next loop, should be driven by ..
Code:
[blue]For Each currRow In ActiveDocument.Tables(intTable)[highlight].Rows[/highlight][/blue]

I think if you change the above, you should find you get as far as processing the individual characters in each Cell.

Now, it is good practice, when you are deleting elements, to start at the end of a collection and work backwards - this avoids missing out elements because of relative movement after deletion. You are going to have a problem with deleting the leading paragraph marks because of this and I would suggest a different approach, something like ..
Code:
[blue].Range.Characters(1).Select
If Selection = vbCR Then
    Selection.MoveEndWhile vbCr
    Selection.delete
End If[/blue]

Finally the deletion of the trailing paragraph marks. Two problems here - one, you need to reset [purple]intCharCount[/purple] to account for the fact that you may have deleted some characters from the beginning (although if you follow my suggestion above all you need to do is make sure you set it once after the deletion) - and, two, to allow for the end of cell marker, you want to start your loop on the last-but-one character ..
Code:
[blue]For intChar = intCharCount[highlight] - 1[/highlight] To 1 Step -1[/blue]

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
 
Hi Tony,

I've tried to make the changes per your suggestions above, but I'm still not doing something right.

Here's my code now with the line I get hung up on highlighted:

Code:
Dim intTableCount As Integer
Dim intTable As Integer
Dim currRow As Row
Dim currCell As Cell
Dim intCharCount As Long
Dim intChar As Long

' Count number of tables in the document
intTableCount = ActiveDocument.Tables.Count

' In each table:
For intTable = 1 To intTableCount
    ' Check each cell in each row of the table
    For Each currRow In ActiveDocument.Tables(intTable).Rows
        For Each currCell In currRow.Cells
            With currCell
                ' Count number of characters in the cell
                [COLOR=black yellow]intCharCount = .Range.Characters(intChar).Count[/color]
                ' STEP 1: Starting with the FIRST character in the cell,
                ' check each character to see if it is a paragraph mark
                .Range.Characters(1).Select
                If Selection = vbCr Then
                    Selection.MoveEndWhile vbCr
                    Selection.Delete
                End If
            End With
        Next currCell
    Next currRow
    
    For Each currRow In ActiveDocument.Tables(intTable).Rows
        For Each currCell In currRow.Cells
            With currCell
                ' STEP 2: Starting with the LAST character in the cell,
                ' check each character to see if it is a paragraph mark
                For intChar = intCharCount - 1 To 1 Step -1
                    ' If the character is a paragraph mark, need to delete it
                    If .Range.Characters(intChar) = vbCr Then
                        .Range.Characters(intChar).Delete
                    ' If the character is not a paragraph mark, break out of
                    ' the loop and check the next cell
                    ElseIf .Range.Characters(intChar) <> vbCr Then
                        Exit For
                    End If
                Next
            End With
        Next currCell
    Next currRow
Next intTable

I apologize for not quite understanding everything. I am teaching myself VBA and some things (like ranges) are really hard for me to understand!

Your help is greatly appreciate!

Cheryl
 
Have you tried this ?
intCharCount = .Range.Characters.Count
But I think it's unnecessary here as you can try this in your 2nd step:
For intChar = .Range.Characters.Count - 1 To 1 Step -1


Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Hi Cheryl,

[blue].Range.Characters[/blue] is a Collection of ALL the Characters in the Range. It has a .Count Property which returns the number of elements (characters) in the Collection.
[blue].Range.Characters(intChar)[/blue], on the other hand, is a single character from the Collection. It makes no sense for it to have a Count property - there is nothimg to count.

What you want to control your loop is the total number of characters so you should use the first construct:

[blue][tt]intCharCount = .Range.Characters.Count[/tt][/blue]

NOTE, however, as I said before, you need to make sure that intCharCount is set correctly when you want to use it which is not where you have it at the moment (before checking the beginning of the cell) but immediately before the innermost for loop in step 2 (before checking the end of the cell).

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
 
This goes through all tables in a document, removing blank paragraphs. This avoids going through row and column counts, by using a cell object.

Sub RemoveBlankPara()

Dim aDoc As Document
Dim i As Integer, TableCount As Integer
Dim aCell As Cell
Dim var As Variant, var2 As Variant

Set aDoc = ActiveDocument
TableCount = aDoc.Tables.Count

If TableCount = 0 Then
Exit Sub ' no point if there are no tables
Else
For var = 0 To TableCount
If Selection.Information(wdWithInTable) = False Then
Selection.GoToNext What:=wdGoToTable
End If
On Error Resume Next
' select each cell in current table and count paragraphs
' collapse to beginning and expand through each paragraph
' delete if paragraph = vbCr
' otherwise, collapse to end and continue
For Each aCell In aDoc.Bookmarks("\table").Range.Cells
aCell.Select
i = Selection.Paragraphs.Count
Selection.Collapse Direction:=wdCollapseStart
For var2 = 1 To i
Selection.Expand Unit:=wdParagraph
If Selection.Text = vbCr Then
Selection.Delete
Else
Selection.Collapse wdCollapseEnd
End If
Next
Next aCell
' move out of current table
Selection.Move Unit:=wdCharacter, Count:=1
Next ' goes to next table in tablecount
End If
Set aDoc = Nothing
End Sub

Gerry
 
Hi Gerry,

I did think of going directly to Cells without the intervening Rows, but Cheryl already had code to use the Rows and I didn't want to complicate things.

A couple of points about your, otherwise fine, code

1. It doesn't remove the final paragraph mark in a cell because it is not a paragraph by itself.
2. More seriously, it does remove empty paragraphs in the middle of the cell which wasn't part of the original request.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
 

I don't know how I did it, but I got the macro to work!!​

The macro now searches each table and removes just the blank paragraph marks at the start of each cell (if any) and the blank paragraph marks at the end of each cell (if any), leaving paragraph marks alone if they are between text. Here's my code:

Code:
Dim intTableCount As Integer
Dim intTable As Integer
Dim currRow As Row
Dim currCell As Cell
Dim intCharCount As Long
Dim intChar As Long

' Count number of tables in the document
intTableCount = ActiveDocument.Tables.Count

' Starting with the first table:
For intTable = 1 To intTableCount
    ' STEP 1: Starting with the FIRST character in the cell,
    ' check each character to see if it is a paragraph mark
    For Each currRow In ActiveDocument.Tables(intTable).Rows
        For Each currCell In currRow.Cells
            With currCell
                ' Count number of characters in the cell
                intCharCount = .Range.Characters.Count
                For intChar = 1 To .Range.Characters.Count Step 1
                    .Range.Characters(1).Select
                    If Selection = vbCr Then
                        Selection.MoveEndWhile vbCr
                        Selection.Delete
                    End If
                Next
            End With
        Next currCell
    Next currRow
    
    ' STEP 2: Starting with the LAST character in the cell,
    ' check each character to see if it is a paragraph mark
    For Each currRow In ActiveDocument.Tables(intTable).Rows
        For Each currCell In currRow.Cells
            With currCell
                For intChar = .Range.Characters.Count - 1 To 1 Step -1
                    ' If character is paragraph mark, need to delete it
                    If .Range.Characters(intChar) = vbCr Then
                        .Range.Characters(intChar).Delete
                    ' If the character is not paragraph mark, break out
                    ' of the loop and check the next cell
                    ElseIf .Range.Characters(intChar) <> vbCr Then
                        Exit For
                    End If
                Next
            End With
        Next currCell
    Next currRow
Next intTable

If there's a cleaner way to do this, I'd be very interested to know.

Thank you for all your help! I couldn't have done these macros without Tek-Tips!!

Cheryl [2thumbsup]
 
Hi Tony,

Ah, yes it does remove ALL "empty" paragraph marks, 'tis true. And that was indeed not part of the original request. I stand corrected. The reason I put my code that way is that I never have empty (blank) paragraphs. Or I should say, I may have some temporary ones. I consistently use styles, so I never use those "extra" paragraph marks to make spaces between text paragraphs.

In the version I am using it also checks to ensure that paragraphs, WITH text, in the table, use one of the tabletext styles, and nothing else.

Any extra paragraph marks are because I may have created a temporary space for something else. So actually for me this works well, as all text within a table should use a tabletext style - therefore all extra marks should be removed. However, I forgot that using hard returns between paragraphs is common. Ooops. Doh.


Gerry
 
Hi Tony,
I have tried to duplicate the situation in your point #1. When I run it with blank paragraph marks in the last cell, all paragraph marks are removed. The last cell has the cell end mark (which I guess is essentially a paragraph mark in fact), just like all other cells with no text (or graphic either). If there is something other than only a paragraph mark, it leaves it alone.

However, yes, it is true, if there is an empty paragraph mark AFTER the last legitimate text, the code will will not remove it. It doesn't take much to fix that. Each loop has a count of the paragraph marks for that cell (extrapolated from sentence.count). Add a variable that is increased as the loop goes through the paragraphs. Match to the sentence count. If they are equal (i.e. this is the last paragraph) AND the paragraph has text, then move the selection back one character - just before the paragraph mark amd Delete. It removes the paragraph mark and the text ends with the cell end mark.

If they are equal AND the paragraph does not have text, then the mark is deleted like all the rest.


Gerry
 
Hi Gerry,

On my point #1, I think you are right on all counts. It's just the end-of-cell mark that is a bit of a funny, a Character of length 2 equal to Chr(13)Chr(7).

On my point #2, I'm with you on Styles but if the text is sourced from outside Word, you get what you get.


Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
 
Yes, you get what you get. I have tried for years to get our users to understand the advantages of styles...sigh.

I guess the only difference in our code is that I use the cell object, and the content of each selected paragraph, rather than parsing through all characters. Personally I feel, as the issue is paragraphs (either "empty" or not), that dealing with paragraphs is cleaner than dealing with characters. But hey, what ever works. I hope Cheryl at least has something that works for her.

Quite interesting that cell end mark. A character of 2. Never thought of it before. Just a wee piece of knowledge, but even that is appreciated. Thanks!


Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top