How : Adding Multi tables on Word with .net

前端 未结 1 1667
伪装坚强ぢ
伪装坚强ぢ 2021-02-06 18:43

I try to add multi tables within a word document using c#

// Tables is a list of items which I want to present each in a table
foreach (List Clas         


        
相关标签:
1条回答
  • 2021-02-06 19:21

    The line

    tbl = MyDoc.Tables.Add(myRange, ClassTable.Count(), 3, missing, missing); 
    

    throws an exception the 2nd time it's executed with the message "The range cannot be deleted". This exception is swallowed by Word but does stop further execution. Addin a try/catch and setting a breakboint would have helped you.

    I edited your code to the following to reproduce and find the exception that was raised:

        var myRange = Globals.ThisAddIn.Application.ActiveDocument.Range();
        foreach (List<List<string>> ClassTable in new List<List<List<string>>> { new List<List<string>> { new List<string> { "A" }, new List<string> { "B" } }, new List<List<string>> { new List<string> { "C" }, new List<string> { "D" } } })
        {
            // tbl is a "Microsoft.Office.Interop.Word.Table"            
            // myRange is like MyDoc.Range(ref missing, ref missing)            
            Microsoft.Office.Interop.Word.Table tbl = null;
            try
            {
                tbl = Globals.ThisAddIn.Application.ActiveDocument.Tables.Add(myRange, ClassTable.Count(), 3);
    
                tbl.Borders.Enable = 1;
                int RowCounter = 1;
                foreach (var item in ClassTable)
                {
                    int ColumnCounter = 1;
                    foreach (string str in item)
                    {
                        tbl.Cell(RowCounter, ColumnCounter).Range.Text = str;
                        ColumnCounter++;
                    }
                    RowCounter++;
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }
     }
    

    The docs at msdn state:

    Required Range object. The range where you want the table to appear. The table replaces the range, if the range isn't collapsed.

    What turns out to be needed is that you 'move' to the end of the range by collapsing it. If you do so you will run into another word issue that if you have 2 tables directly after eachother in a document word will automagically join them into 1 table. Your fixed code would end up adding more and more rows to 1 table and constantly overwrite the first few rows with the values. All of this leads to the following code that should fix your problem:

        var myRange = Globals.ThisAddIn.Application.ActiveDocument.Range();
        foreach (List<List<string>> ClassTable in new List<List<List<string>>> { new List<List<string>> { new List<string> { "A" }, new List<string> { "B" } }, new List<List<string>> { new List<string> { "C" }, new List<string> { "D" } } })
        {
            // tbl is a "Microsoft.Office.Interop.Word.Table"            
            // myRange is like MyDoc.Range(ref missing, ref missing)            
    
            Microsoft.Office.Interop.Word.Table tbl = null;
            try
            {
                tbl = Globals.ThisAddIn.Application.ActiveDocument.Tables.Add(myRange, ClassTable.Count(), 3);
    
                tbl.Borders.Enable = 1;
                int RowCounter = 1;
                foreach (var item in ClassTable)
                {
                    int ColumnCounter = 1;
                    foreach (string str in item)
                    {
                        tbl.Cell(RowCounter, ColumnCounter).Range.Text = str;
                        ColumnCounter++;
                    }
                    RowCounter++;
                }
    
                // Move to the end
                myRange.Collapse(Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd);
                // Now add something behind the table to prevent word from joining tables into one
                myRange.InsertParagraphAfter();
                // gosh need to move to the end again
                myRange.Collapse(Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd);
    
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }
    
        }
    

    One last warning is that the first line in this segment reads:

    var myRange = Globals.ThisAddIn.Application.ActiveDocument.Range();
    

    Adding a table to this range will work if the document is empty otherwise it will throw the same exception since we are not at the end in that case. A .Collapse() would resolve it there as well.

    0 讨论(0)
提交回复
热议问题