如何在word文档中的邮件合并期间从字段代码访问父表

时间:2016-06-03 16:43:26

标签: c# ms-word docx openxml-sdk mailmerge

I have a docx word document that contains table and table cell with mergefield inside.

enter image description here

我想将表格单元格的背景更改为橙色值(#EA6E24),但我找不到字段和方法。

所以这是我的代码处理字段的邮件合并并尝试向表格单元格添加阴影。

 fldCode = parent.Descendants<FieldCode>().FirstOrDefault();

 if (fldCode != null && fldCode.InnerText.Contains("MERGEFIELD") &&
 {
    var ancestors = fldCode.Ancestors<TableCell>();

    // trouver et colorier le parent
    fieldName = "";

    var shading = new Shading()
    {
        Color = "auto",
        Fill = "#EA6E24",
        Val = ShadingPatternValues.Clear
    };

    foreach (var ancestor in ancestors)
    {
        ancestor.Append(shading);
    }                 
 }

1 个答案:

答案 0 :(得分:0)

解决方案是访问并覆盖着色属性。

 foreach (var ancestor in ancestors)
   {
         var tableCellProperties = ancestor.TableCellProperties;
         tableCellProperties.Shading = shading;                            
   }