简体   繁体   中英

How to add a field with formatting to MS Word using C#

I have a word document which contains a custom property ("MyCustomProperty"). I would like to use C# to insert a DOCPROPERTY field with formatting and highlighting. This is what I have tried...

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();
}

When I run this code, the field gets added to the header and is right-aligned. But the font, size, and weight are all default (Calibri (body), 11, not bold, not italic). The text is not highlighted.

What I would like is for the field to be added, right-aligned, on a line by itself, with the font, size and weight I've configured.

What am I doing wrong?

Writing Word documents with the object model is not very intuitive to me. Here is what I did to solve my problem...

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);

If you have a better suggestion, I'm all ears.

I had the same problem, your Answer is correct, but another solution is to just set the properties after you added the field, but on the original Range (or just get the reference again as you do)

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM