Word VBA >> Word Table and Page Break

by anonymous » Tue, 03 Aug 2004 19:56:50 GMT

I wrote a macro that looks at a column in a word table.
Starting at the bottom of the table, it looks at each of
the cells and if the cell matches the one above it, it
removes the lower cell and continues.

My problem is that the table breaks between page one and
two and when the macro runs, it automatically deletes all
items in the third row down on the second page. They do
not match any other rows and it will delete them
regardless of what they say. If I lengthen the page so
that the entire table fits on one page, the macro works
great, so I am assuming that it is the page break in the
table the causes the problem. I did
add 'Application.ScreenUpdating = False' but this only
caused it to delete the second instead of the third line.

The code is below:

Dim tablecell, counter, head1, head2, head3

Selection.Tables(1).Select
Selection.MoveUp Unit:=wdLine, Count:=1
Selection.SelectCell
head1 = Selection
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.SelectCell
head2 = Selection
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.SelectCell
head3 = Selection

counter = 0
' Identify which column we are working with
10 Selection.Tables(1).Select
Selection.MoveDown Unit:=wdLine, Count:=1
counter = counter + 1
Selection.MoveUp Unit:=wdLine, Count:=1
Selection.SelectCell

' For first column
If counter = 1 Then
GoTo 20
ElseIf counter = 2 Then
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.SelectCell
GoTo 20
ElseIf counter = 3 Then
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.SelectCell
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.SelectCell
End If

' Remove duplicates from the column we are working with
20 tablecell = Selection
Selection.MoveUp Unit:=wdLine, Count:=1
Selection.SelectCell

' Stop process if at the top cell
23 If Selection = head1 Then
GoTo 10
ElseIf Selection = head2 Then
GoTo 10
ElseIf Selection = head3 Then
GoTo 40
End If

' Delete the duplicates
30 If Selection <> tablecell Then
Selection.SelectCell
GoTo 20
ElseIf Selection = tablecell Then
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.SelectCell
Selection.Delete
Selection.MoveUp Unit:=wdLine, Count:=1
Selection.SelectCell
GoTo 20
End If

40 End



Word VBA >> Word Table and Page Break

by Jezebel » Tue, 03 Aug 2004 20:45:48 GMT


our code is a case study in why not to use GOTO instructions ... it creates
logical spaghetti that is almost impossible to follow. Coupled with the
relentless use of the Selection object ... disaster, as you've found.
Specifically, your problem is that those Move instructions fall foul of the
page break: yet another reason to work directly with the underlying object.

Assuming your tables are uniform (no split or merged cells), try something
like this:

Const pColumn as long = 1 ' or whatever column you are testing
Dim pRow as long

With Selection.Tables(1)

'Check the rows starting from the bottom
For pRow = .Rows to 2 step -1

'Compare the cell with the one above
if .Cell(pRow, pColumn).Range = .Cell(pRow -1 , pColumn).Range then

'Matches, so clear the cell
.Cell(pRow, pColumn).Range = ""

end if
Next

End with




< XXXX@XXXXX.COM > wrote in message
news:a21001c47950$f5c29090$ XXXX@XXXXX.COM ...





Word VBA >> Word Table and Page Break

by anonymous » Wed, 04 Aug 2004 00:04:31 GMT

hanks for the info. I had to add (.count) after (.rows)
but other than that it worked great. Again, I appreciate
the help.

instructions ... it creates
Coupled with the
you've found.
instructions fall foul of the
underlying object.
cells), try something
you are testing
pColumn).Range then
all
line.


Similar Threads

1. Problem w/tables breaking across page break in word 2007

2. Word table properties should include "no page break in table" - MS Word

3. How to prevent breaking a table row across page breaks

I am using vb6 to control Word and have a table that spans 
multiple print pages.  How can I prevent Word from 
breaking a row, with multiple lines across page breaks?

Thanks for any advice in advance,

WayneM

4. preventing tables from being broken by pages breaks - MS Word

5. Page break without breaking table

I have a table I want to break over two pages but I want to break the
table before it reaches the bottom margin. Inserting a page break into
a table cell seems to break the table into two seperate tables, one on
each page, but I need to maintain the consistency of the table.

Can this be done without changing row heights?

thanks in advance

Craig

6. How to avoid page break in word page contains tables - - MS Word Support

7. read per word until the end page(page break)

Hi,

I tried to read per word from my table of content below and write per 
paragraph
 into the text file.
It's not working since it only read the first 3 lines and after that I
always get 'bad parameter' message error due to tab error in the table of 
content like:
1.3 Test
    1.3.1 Error read this line.
Would anyone know how to read per word and write value per line in text file 
until the
page break(end of page)?
Please help me...!

Thank you so much...
John

Part II   Test Cases 8

1    Central Management Console (CMC) 8

1.1   CMC Sanity Check 8

1.2   Folders 10

1.3   Crystal Report 18

1.3.1    Add Report 19

1.3.2    Preview Report 23

1.4   Program and 3rd Party Objects 29

1.4.1    Add Objects 29

1.4.2    Program Object Viewing. 31

1.5   Categories 33

1.5.1    Create Category. 34

1.5.2    Modify Category. 35

1.5.3    Delete Category. 37

1.5.4    Assign Category. 38

1.5.5    Move Category. 39

1.5.6    Apply Rights to Category. 41

1.6       Universe (Sanity) 42
--------------------------page break-----------------------

Sub readtableofcontent()
    Dim oRngH As Word.Range
    Dim NextLine As Integer
    Set oRngH = ActiveDocument.Range
    NextLine = 2

        With oRngH.Find
              .Text = "Part II Table of Content"
              .Font.Name = "Verdana"
              .Font.Size = 10
            If .Execute Then
            oRngH.Move wdParagraph, NextLine
            While
(oRngH.Application.ActiveDocument.Paragraphs.Last.Range.Text <> Chr(11))
                oRngH.Move NextLine
                NextLine = NextLine + 1
                oRngH.Expand wdParagraph
                objFile.writeLine (oRngH.Text)
            Wend
            End If
        End With
end sub



8. Need to show the words "Page Break" on each page - Word VBA