0

I want to set caption to my table:

var caption = new TableCaption();
caption.Val = "Test";
table.Append(caption);

But nothing happens. Whats wrong here?

UPADATE

                foreach (var tabl in mainPart.Document.Body.Descendants<Table>())
                {
                    foreach (var trow in tabl.Elements<TableRow>())
                    {
                        foreach (var tcell in trow.Elements<TableCell>())
                        {
                            foreach (var para in tcell.Elements<Paragraph>())
                            {
                                foreach (var run in para.Elements<Run>())
                                {
                                    foreach (var text in run.Elements<Text>())
                                    {
                                        if (
                                            text.Text ==
                                                "my caption")
                                        {
                                            para.RemoveAllChildren();
                                            para.Remove();
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
Kliver Max
  • 5,107
  • 22
  • 95
  • 148
  • Checkout the accepted answer for [this](http://stackoverflow.com/questions/15548298/add-a-caption-to-a-table-or-figure-in-openxml) Hope it helps – LakshmiNarayanan May 26 '14 at 06:38

1 Answers1

2

In your code, I guess the table is a DocumentFormat.OpenXml.Wordprocessing.Table. This is not a valid parent for this element. The TableCaption should be added to a TableProperties element <w:tblPr>.

Table table = new Table();
TableProperties tableProperties = new TableProperties(
                  new TableCaption() { Val = "Test" }
                );
table.AppendChild<TableProperties>(tableProperties);

Then note the <w:tblCaption> element was added in the 2008 version of ECMA-376, so Word 2007 does not support this. The TableCaption class is only available since Office2010.

To add/see this caption in word:

Right-Click on the table->Properties->Text/Replacement

Chris
  • 8,527
  • 10
  • 34
  • 51
  • I tried your code but its not help. In xml file i see:``. But when open document in Word2010 not see caption. – Kliver Max May 27 '14 at 10:56
  • Particvaly i soled my problem. I can find a Caption and change it or delete. Example in question. – Kliver Max May 27 '14 at 11:49
  • @KliverMax Hi, see updated answer. When I said *I confirm it doesn't work*, I was attempting to see the caption in the table in MS Word. But it is a *non visual* element. So after testing with Word 2010 I confirm the caption exists *Right-Click on the table->Properties->Text/Replacement*. From your updated code, it seems you want to access the column header of your table, that is not a *TableCaption*, but a *classic* content of `TableCell` inside a `TableRow`. – Chris May 29 '14 at 10:21