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

Dan Xiao 406 Reputation points
2021-08-02T02:24:20.777+00:00

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

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,249 questions
{count} votes

Accepted answer
  1. Dan Xiao 406 Reputation points
    2021-08-09T12:44:19.103+00:00

    Hi, @Daniel Zhang-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

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Daniel Zhang-MSFT 9,611 Reputation points
    2021-08-03T05:51:34.703+00:00

    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.

    1 person found this answer helpful.
    0 comments No comments

  2. Dan Xiao 406 Reputation points
    2021-08-04T07:22:42.843+00:00

    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