How to work out which Table a Run belongs to

Hi,

I’m using the following code to make sure that every Table in a document is set to Arial font.

foreach(Run run in doc.GetChildNodes(NodeType.Run, true))
{
    if (run.ParentParagraph.IsInCell)
    {
        run.Font.Name = "Arial";
    }
}

This works fine.

However, I’ve now been asked to set the first 8 tables to 12pt and all the rest to 10pt. Is it possible to evaluate which table a Run belongs to? Or would there be a more efficient way of achieving this?

Any assistance gratefully received.

Mark

Hi
Thanks for your request. You can try inserting a Bookmark in a table, which you would like to find. Then you can use the following code to find the table:

Document doc = new Document("in.doc");
// Get bookmark
Bookmark bk = doc.Range.Bookmarks["bookmark"];
if (bk != null)
{
    // Get table, where bookmark is located.
    Node table = bk.BookmarkStart.GetAncestor(NodeType.Table);
    if (table != null)
    // Work with table.
}

Best regards,

Hmm - that doesn’t really tell me which Table a Run is part of…

Hi Mark,
Please see the code below which should achieve what you are looking for.

// All tables in the document.
NodeCollection tableNodes = doc.GetChildNodes(NodeType.Table, true);
foreach(Run run in doc.GetChildNodes(NodeType.Run, true))
{
    Table parentTable = (Table) run.GetAncestor(NodeType.Table);
    // Find and compare the ID of the table which contains this run.
    if (tableNodes.IndexOf(parentTable) <8)
        run.Font.Size = 12;
    else
        run.Font.Size = 10;
}

Thanks,

Also, is appears you can now access the forum. Were you able to solve your browser error on your side?
Thanks,