paragraph format on selected cells in a table

T

tbaam

What am I doing wrong?

I looked at old posts and wrote the following piece of code. It searches a
table for a section of rows and keeps them together. I then want then to stay
together and if necessary go to the next page.

I can the see the lines highlight as expected but the paragraph format does
not seem to be doing what it is suppose to.

Thanks
Sub Macro1()
Dim i
Dim End_row, Start_row, New_row As Integer
Dim num
'num = Selection.Columns(1).Cells.Count
Row = 3
For i = 4 To 400
Value = Left(ActiveDocument.Tables(2).Cell(Row:=i,
Column:=1).Range.Text, Len(ActiveDocument.Tables(2).Cell(Row:=i,
Column:=1).Range.Text) - 2)
If Value < 100 Then
End_row = i
With ActiveDocument.Tables(2)
pos = .Cell(Row, 1).Range.Start
posEnd = .Cell(End_row, 5).Range.End
Set myrange = ActiveDocument.Range(Start:=pos, End:=posEnd)
myrange.Select
'Selection.myrange
With Selection.ParagraphFormat
.WidowControl = True
.KeepWithNext = True
.KeepTogether = True
.PageBreakBefore = False
End With
End With
Row = i
End If
Next i
End Sub
 
J

Jezebel

What am I doing wrong?

1. You haven't defined your objective. It's not obvious from the code
(which, on your own admission, doesn't work anyway).

2. You're trying to apply paragraph formatting to indeterminate ranges
across mulitple table cells. This generally doesn't work. Format the
paragraphs within this range.

3. You can't have read the documentation on variable declarations. Otherwise
you wouldn't have ended up with one obsolete data type and four slow and
inappropriate ones.

4. You're using the Selection object where a Range object would be more
appropriate.
 
T

tbaam

My objective: I have a table that scans multiple pages in Word. At the end
of each page are a series of rows that I am trying to keep together. Once I
determine this set of rows I want to use paragraph formatting and keep with
next.

I went back and read and understand what I did wrong. As the macro runs I
can see the rows being selected. But, the paragraph format does not work. If
I replace the paragraph format, with change font size, I can see that work.
So it has to do with the paragraph format command.

Sub Macro1()
Dim i
StartRow = 3
For i = 4 To 100
Value = Left(ActiveDocument.Tables(2).Cell(Row:=i,
Column:=1).Range.Text, Len(ActiveDocument.Tables(2).Cell(Row:=i,
Column:=1).Range.Text) - 2)
If Value < 100 Then
EndRow = i - 1
MsgBox EndRow
Set myrange = ActiveDocument.Range( _
Start:=ActiveDocument.Tables(2).Cell(StartRow,
1).Range.Start, _
End:=ActiveDocument.Tables(2).Cell(EndRow, 5).Range.End)
myrange.Select
With myrange.ParagraphFormat.KeepWithNext = True
End With
End If
StartRow = EndRow + 1
Next i
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top