question

DanielXiaoDan-2510 avatar image
0 Votes"
DanielXiaoDan-2510 asked TimonYang-MSFT commented

How to prevent a table from splitting over two pages in a Word document?

Dear Sir/Madam,

I write a C# program, to populate data into a table in a word template (see the Word file WordTemplate.docx in the package zip file Post_05Jun21_Table_Split_Over_Two_Pages.zip under the OneDrive hyperlink https://onedrive.live.com/?id=8FFA4A1C66CFCED9%211145&cid=8FFA4A1C66CFCED9)

However, the table would all the way split over to two pages in the output Word document (see the output Word file WordOutputSplitToMultiPages.docx and the screenshot file Table_split_two_pages.jpg in the uploaded package zip file Post_05Jun21_Table_Split_Over_Two_Pages.zip from the above-mentioned OneDrive folder).

I have googled many solutions, but all of them cannot work. Some of the sample solutions are shown below:

https://word.tips.net/T001827_How_to_Stop_a_Table_Row_from_Splitting_Over_Two_Pages.html
https://techcommunity.microsoft.com/t5/word/word-table-splitting-across-pages/m-p/1362024
https://fiveminutelessons.com/learn-microsoft-word/stop-table-rows-microsoft-word-splitting-across-pages
https://www.extendoffice.com/documents/word/4543-word-keep-table-rows-on-same-page.html
https://www.myofficetricks.com/how-to-prevent-tables-from-breaking-across-pages/

Can anybody provide a solution to enhance the WordTemplate.docx file to prevent the table from splitting over two pages?

Many thanks.







dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

TimonYang-MSFT avatar image
0 Votes"
TimonYang-MSFT answered TimonYang-MSFT commented

I am using Office2016. If I select the entire table and check Keep With Next in the options, the table will move down to the next page.

If you want to do this in your code, try the following code:

            Application application = new Application();
             Microsoft.Office.Interop.Word.Document document = application.Documents.Open(@"C:\...\1.docx");
             try
             {
                 var table = document.Tables[1];
                 Microsoft.Office.Interop.Word.ParagraphFormat pf = table.Range.ParagraphFormat;
                 pf.KeepWithNext = -1;
             }
             catch (Exception e)
             {
                 Console.WriteLine(e);
    
             }
             finally
             {
                 document.Close();
                 application.Quit();
             }

Update(6/15):
Do you want to treat the content in the picture as a table?

105692-1.png

This doesn't work, Excel treats them as two.

In the current situation, the second table will meet our expectations. It will monopolize a page as much as possible, which will cause it to be easily separated from the first table.

After various attempts, I found that the current second table seems to have the first row set as the header, but for some reason, when we fill the table with data, it is not.

I have to unset it as a header, and then set it as a header for it to take effect.

Reflected in the code, like this:

             Application application = new Application();
             Document document = application.Documents.Open(@"C:\Users\timony\Desktop\WordTemplate.docx");
             try
             {
                 int count = document.Tables.Count;
    
                 Row row = document.Tables[2].Rows[1];
                 row.HeadingFormat = 0;
                 row.HeadingFormat = -1;
             }
             catch (Exception e)
             {
                 Console.WriteLine(e); ;
             }
             finally 
             {
                 document.Save();
                 document.Close();
                 application.Quit();
                 Console.WriteLine();
             }

Filling in the data after this does not seem to separate the two tables.


If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1.png (26.3 KiB)
1.png (12.2 KiB)
· 14
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi, Timon,

I would try your solution later on.

Thanks.

0 Votes 0 ·

Hi, TimonYang-MSFT,

Just now I follow up your solution on both the Word Template file and C# codes, but unfortunately there is not any improvement, and the table would still spit over to next page. (see the uploaded sample output Word document https://onedrive.live.com/edit.aspx?cid=8ffa4a1c66cfced9&page=view&resid=8FFA4A1C66CFCED9!1147&parId=8FFA4A1C66CFCED9!1145&app=Word in OneDrive portal.

Can you provide any more solutions?
Thanks.

Rgds,
Daniel

0 Votes 0 ·

Hi, @DanielXiaoDan-2510, I cannot get the sample file.

103349-capture.png

Did the upload fail?

0 Votes 0 ·
capture.png (12.2 KiB)

Hi, Timon,

I re-uploaded both the Word Template file and the output Word document into OneDrive folder. Their hyperlinks are shown below:

Word Template file

https://onedrive.live.com/edit.aspx?cid=8ffa4a1c66cfced9&page=view&resid=8FFA4A1C66CFCED9!1151&parId=8FFA4A1C66CFCED9!1149&app=Word

Output Word Document

https://onedrive.live.com/edit.aspx?cid=8ffa4a1c66cfced9&page=view&resid=8FFA4A1C66CFCED9!1150&parId=8FFA4A1C66CFCED9!1149&app=Word


Pls kindly let me know whether you can access & download them.
Thanks.

Rgds,
Daniel


0 Votes 0 ·

Unfortunately, I still can't see it.
Is it possible to upload it to GitHub?

0 Votes 0 ·

Hi, TimonYang,

Later on I will try to upload to a GitHub account.
Thanks.

Rgds,
Daniel

0 Votes 0 ·

Hi, TimonYang-MSFT,

Just now I have uploaded the two Word files into GitHub, and their hyperlinks are as below.

Word Template

https://github.com/DanielXiao2018/MicrosoftPost/blob/main/WordTemplate.docx

Output Word Document

https://github.com/DanielXiao2018/MicrosoftPost/blob/main/WordOutputSplitToMultiPages.docx


Please check whether you can view the two Word files. If you still cannot view them, do you mind giving me your email address so that I can directly send to you by an email?

Thanks for the great help!

Rgds,
Daniel

0 Votes 0 ·

@DanXiao-7645
I updated the answer, please check it.

0 Votes 0 ·

Hi @DanXiao-7645, may I know if the content and code I added on 6/15 might be helpful? If there is something wrong with this code, please let me know so we can continue to discuss this issue.

0 Votes 0 ·

Hi, TimonYang-MSFT,

Good news: after applying your C# codes on the update of 6/15, the long-pending Word split-over issue has got fully resolved.

Thank you for the great help and sorry for the late response.

Rgds,
Daniel

0 Votes 0 ·
Show more comments
Show more comments