如何使用C#向MS Word添加带格式的字段

时间:2014-08-28 15:06:24

标签: c# ms-word

我有一个word文档,其中包含一个自定义属性(" MyCustomProperty")。我想使用C#插入带有格式和突出显示的DOCPROPERTY字段。这就是我试过的......

var myCustomProperty = "MyCustomProperty";
foreach (Microsoft.Office.Interop.Word.Section section in Document.Sections)
{
    var headerRange = section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
    foreach(Word.Field field in headerRange.Fields)
    {
        if(field.Type == Word.WdFieldType.wdFieldDocProperty
         && field.Code.Text.Contains(myCustomProperty))
        {
            //already has the header
            return;
        }
    }
    headerRange.Collapse(Word.WdCollapseDirection.wdCollapseStart);
    var f = (Word.Field)headerRange.Fields.Add(headerRange,
                           Word.WdFieldType.wdFieldDocProperty,
                           myCustomProperty,
                           true);

    f.Code.Font.Name = this.FontName;
    f.Code.Font.Size = this.FontSize;
    f.Code.Font.Bold = (int)this.IsBold;
    f.Code.Font.Italic = (int)this.IsItalic;
    f.Code.HighlightColorIndex = Word.WdColorIndex.wdYellow;
    f.Update();
    f.Code.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
    f.Code.InsertParagraphAfter();
}

当我运行此代码时,该字段将添加到标题中并且右对齐。但字体,大小和重量都是默认值(Calibri(正文),11,不是粗体,不是斜体)。文本未突出显示。

我希望将字段添加,右对齐,单独排列,并配置我已配置的字体,大小和重量。

我做错了什么?

2 个答案:

答案 0 :(得分:1)

使用对象模型编写Word文档对我来说不是很直观。以下是我为解决问题所做的工作......

Word.Section section = Document.Sections[1];

var headerRange = section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
foreach(Word.Field field in headerRange.Fields)
{
    if(field.Type == Word.WdFieldType.wdFieldDocProperty
      && field.Code.Text.Contains(myCustomProperty))
    {
        //already has the header
        return;
    }
}

headerRange.Collapse(Word.WdCollapseDirection.wdCollapseStart);
headerRange.InsertParagraphBefore();
headerRange = section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
headerRange.Collapse(Word.WdCollapseDirection.wdCollapseStart);
headerRange.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;

headerRange.Font.Name = this.FontName;
headerRange.Font.Size = this.FontSize;
headerRange.Font.Bold = (int)this.IsBold;
headerRange.Font.Italic = (int)this.IsItalic;
headerRange.HighlightColorIndex = Word.WdColorIndex.wdYellow;

var f = (Word.Field)headerRange.Fields.Add(headerRange,
                        Word.WdFieldType.wdFieldDocProperty,
                        myCustomProperty,
                        true);

如果你有更好的建议,我会全力以赴。

答案 1 :(得分:1)

我有同样的问题,你的答案是正确的,但另一个解决方案是在你添加字段之后设置属性,但是在原始范围上(或者只是像你那样再次获得参考)

Word.Table fTable = footerRange.Tables.Add(footerRange, 1, 3);
cellRange = fTable.Cell(1, 3).Range;
cellRange.Collapse(Word.WdCollapseDirection.wdCollapseStart);
cellRange.Fields.Add(cellRange, Word.WdFieldType.wdFieldDate);

// this is the important part, set the fontName again on the original Range (don't use cellRange)
fTable.Cell(1, 3).Range.Font.Name = FontName;