0

I have developed a WinForms application which utilises the DocX Library for editing .docx files in memory.

I have one issue which i have searched Google vigorously, this is I would like to add a Table to my document using a specific index.

I do this by already having a blank table in the document and then finding the index and replacing the table with my table from my app using the index from the pre-existing table. I end up with this error:

Location: Part: /word/document.xml, Line: 22, Column: 0

I do not know if this is a DocX glitch or that I have approached this wrong.

The code below is not my app but some basic code which my app utilises.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Novacode;

namespace DocXTesting
{
class Program
{
    static void Main(string[] args)
    {

        using (DocX document = DocX.Load(@"Test.docx"))
        {
            Table t = document.Tables[0];
            int tIndex = t.Index;

            try
            {
                t.Remove();

                Table newTable = document.InsertTable(tIndex, 4, 4);
                document.Save();

            }
            catch (Exception x)
            {
                Console.WriteLine(x.Message);
            }

        }
        Console.ReadKey();

    }
}

}

user2345709
  • 11
  • 1
  • 4
  • I answered a similar question here: http://stackoverflow.com/questions/24534638/docx-clone-table-and-insert-at-index/25917483#25917483 Hope it helps. – Vilhelm Sep 18 '14 at 16:12

1 Answers1

1

According to the "Advance - Invoice Example" example project that can be downloaded from codeplex (http://docx.codeplex.com/releases/view/31554):

  1. Find the placeholder table
  2. Insert new table after the placeholder table
  3. Remove the placeholder table

Simplified code:

Table placeholderTable = document.Tables[0];

Table realTable = placeholderTable.InsertTableAfterSelf(4, 4);

placeholderTable.Remove();

If you need to replace a text with a table, see Vilhelm's answer DocX clone table and insert at index

Community
  • 1
  • 1
Steven Manuel
  • 1,898
  • 1
  • 18
  • 22