question

DanXiao-7645 avatar image
0 Votes"
DanXiao-7645 asked ozzie-eu commented

How to merge two tables in a Word document using C# Word Interop programmatically

119629-merge-two-tables.jpg



Dear all,

I want to merge two tables (see the screenshot in the attachment) in a Word document, by using C# Word Interop. The basic solution is to identify the paragraph of the gap between these two tables, and then delete the paragraph. However, I have no idea on how to identify the paragraph and then how to delete it (by its Range?).

The sample Word document file Sample_Word_Doc.docx is uploaded in the hyperlink https://onedrive.live.com/edit.aspx?cid=8ffa4a1c66cfced9&page=view&resid=8FFA4A1C66CFCED9!1157&parId=8FFA4A1C66CFCED9!1156&app=Word

(It is so strange that in a Microsoft community portal, a Microsoft's Word document cannot be uploaded.)

Thanks a lot for any help.

Rgds,
Daniel

dotnet-csharp
merge-two-tables.jpg (114.7 KiB)
· 4
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.

@DanXiao-7645
As tag "office-word-itpro" focus more on general issue of Word client, but your issue is more related to Word development, I would remove it. Thanks for your understanding.

0 Votes 0 ·

Hi @DanXiao-7645,
We do not support email replies. In order to protect your personal information, I have deleted the comments containing your email information.
You can share your Word document through OneDrive.
When deleting the paragraph in the middle of the two tables, Word will automatically merge the two tables into one. And you cannot insert the cursor and enter text, so you have to split into two tables to insert the text.
Best Regards,
Daniel Zhang

0 Votes 0 ·

Hi @DanXiao-7645,
May I know whether your issue has been solved or not? If not, please share it in here. We can work together to figure it out.
Best Regards,
Daniel Zhang

0 Votes 0 ·

Resolved. See my post 10 days agao.
Thanks.

0 Votes 0 ·
DanXiao-7645 avatar image
0 Votes"
DanXiao-7645 answered DanXiao-7645 published

Hi, @DanielZhang-MSFT,

I am using Microsoft Visual Studio 2019. Actually I have managed to delete the gap and merge two tables, just by writting the Delete() statement twice.

My sample C# codes are shown below.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void deleteGapText(Word.Document doc, string txt)
{
object missing = System.Reflection.Missing.Value;
doc.Content.Find.ClearFormatting();
object keyword = txt.ToString();
if (doc.Content.Find.Execute(ref keyword, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing))
{
var range = doc.Content;
if (range.Find.Execute(txt))
{
range.Expand(Word.WdUnits.wdParagraph); // or change to .wdSentence or .wdLine
range.Delete();
// TH, 04Aug21, to completely merge the two adjacent tables
range.Expand(Word.WdUnits.wdParagraph); // or change to .wdSentence or .wdLine
range.Delete();
// Word.Paragraph para = app.Selection.Range.Paragraphs[1];
// tbl.Rows.WrapAroundText = -1;
}
}
else
{
log_msg(Settings.Default.LogFile, string.Format("deleteGapText(): TableGapText ({0}) cannot be found.", txt));
}
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Anyway, thank you for the help anyway.
I would select to "Accept Answer".

Rgds,
Daniel

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.

DanielZhang-MSFT avatar image
1 Vote"
DanielZhang-MSFT answered DanielZhang-MSFT edited

Hi DanXiao-7645,
You can try to upload your word document via Attachment icon.
120007-83.png
And the hyperlink you provided indicates that this item may not exist or is no longer available.
>>I have no idea on how to identify the paragraph and then how to delete it (by its Range?).
You can delete this paragraph by specific words in the paragraph.
Here is a code example you can refer to.

 void searchText(string txt)
         {
             Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
             Microsoft.Office.Interop.Word.Document doc = app.Documents.Open("C:\\Users\\test.docx");
             object missing = System.Reflection.Missing.Value;
             doc.Content.Find.ClearFormatting();
             object keyword = txt.ToString();
             if (doc.Content.Find.Execute(ref keyword, ref missing, ref missing, ref missing, ref missing, ref missing,
                 ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing))
             {
                 var range = doc.Content;
                 if (range.Find.Execute(txt))
                 {
                     range.Expand(WdUnits.wdParagraph); // or change to .wdSentence or .wdLine
                     range.Delete();
                 }
             }
             else
             {
                 MessageBox.Show("Not found");
             }
             doc.Close(ref missing, ref missing, ref missing);
             app.Quit(ref missing, ref missing, ref missing);
         }

Best Regards,
Daniel Zhang


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.



83.png (7.0 KiB)
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.

DanXiao-7645 avatar image
0 Votes"
DanXiao-7645 answered DanielZhang-MSFT commented

Hi, Daniel Zhang,

Thank you for the reply.

"You can share your Word document through OneDrive."

Originally, I had uploaded the sample Word document into my OneDrive account, but you cannot access it. Do you have a OneDrive account? It seems that I may need to specifically set an access permission for your OneDrive account.

As per your solution, today I tested, but found that it can only delete the specified text, but fail to delete the paragraph in the gap between the two tables, so that the two tables still cannot be merged as expected.

The screenshot of the original Word Template with text in the gap between two tables is attached below.

120403-original-wordtemplate-with-text-in-gap-between-two.jpg


The screenshot of the table gap after running your C# codes is attached below.

120340-remove-text-but-not-paragraph-after-running-the-pr.jpg


My customized C# source codes are also attached here for your reference.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void deleteGapText(Word.Document doc, string txt)
{
object missing = System.Reflection.Missing.Value;
doc.Content.Find.ClearFormatting();
object keyword = txt.ToString();
if (doc.Content.Find.Execute(ref keyword, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing))
{
var range = doc.Content;
if (range.Find.Execute(txt))
{
range.Expand(Word.WdUnits.wdParagraph); // or change to .wdSentence or .wdLine
range.Delete();
}
}
else
{
log_msg(Settings.Default.LogFile, string.Format("deleteGapText(): TableGapText ({0}) cannot be found.", txt));
}
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

My question is to delete the gap between two tables completely, do I need to issue a delete() statement again? I get the hints from my manual operation on a Word document to delete the gap and merge two tables. This is because my manual operations replicate the same scenario as the C# codes: after pressing the "Delete" button at first time, only the text is deleted, but the paragraph is still there; only after I manually select the entire paragraph in the gap and press the "Delete" button, then then gap can be removed and the two tables can be merged together.

Thanks for any further help.

Rgds,
Daniel


· 1
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 @DanXiao-7645,
I searched some information and did some test and it is hard to delete the gap with Word Interop.
And you can try the to use FreeSpire. Doc.
Here is a code example:

  Document doc = new Document();
             doc.LoadFromFile(@"C:\Users\test.doc");
    
             foreach (Section section in doc.Sections)
             {
                 for (int i = 0; i < section.Body.ChildObjects.Count; i++)
                 {
                     if (section.Body.ChildObjects[i].DocumentObjectType == DocumentObjectType.Paragraph)
                     {
                         if (String.IsNullOrEmpty((section.Body.ChildObjects[i] as Paragraph).Text.Trim()))
                         {
                             section.Body.ChildObjects.Remove(section.Body.ChildObjects[i]);
                             i--;
                         }
                     }
    
                 }
             }
             string result = @"C:\Users\test.doc";
             doc.SaveToFile(result, FileFormat.Docx2013);

Best Regards,
Daniel Zhang

0 Votes 0 ·