0

Before you say this has already be answered it really has not anywhere i have found. This is easy to do with a bunch of text, but not easy to do with tables. You can make rows stay on one page, but not the whole table.

To even do this in word you have to select all but the last row of the table and then use keep with next. I can do this with the following code

foreach (Word.Table tb in objApp.ActiveDocument.Tables)
{
    Range rng = objDoc.Range(tb.Rows[1].Range.Start, tb.Rows[tb.Rows.Count - 1].Range.End);
    rng.Select();         
}

Now i have no idea how to apply a paragraph style to keep with next to each one of these. You cannot roll though every table in a paragraph, although each of mine contain only one table. So i am lost.

Thanks to anyone that can help

Allix
  • 170
  • 2
  • 17
Scott Wein
  • 5
  • 1
  • 4
  • you put a paragraph around it with keep together on it – BugFinder May 16 '18 at 10:08
  • it does not work inline tried it. It still spills over flipping the switch as it is made. do you have any other suggestion? } Range rng = objDoc.Range(objTab1.Rows[1].Range.Start, objTab1.Rows[objTab1.Rows.Count - 1].Range.End); rng.Select(); objPara2.KeepWithNext = -1; } – Scott Wein May 16 '18 at 13:31
  • [Related question](https://stackoverflow.com/questions/20741729/interop-prevent-a-table-wrapping-over-two-pages-in-word) – Sen Jacob Apr 04 '19 at 13:53

1 Answers1

0

There are various ways you can approach applying the formatting to all the paragraphs in the table. One very good one would be to apply a STYLE to the table, as that is most efficient and most easily altered if something in the text formatting of the entire table needs to be changed.

However, a more direct method would be to apply the formatting to the entire table Range:

Word.ParagraphFormat pf = objTab1.Range.ParagraphFormat;
pf.KeepWithNext = true;
pf.KeepTogether = true;

In order to apply a style in one step:

objTab1.Range.Style.set_Style("Style name");
Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
  • This is the first time i have seen something with this that actually works. thank you. I dont think a style would work though as you actually have to select the table except last line. but this actually works inline. – Scott Wein May 16 '18 at 23:21
  • @ScottWein See my edit for using a style in one step... :-) – Cindy Meister May 17 '18 at 17:18