简体   繁体   English

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

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

I have a word document which contains a custom property ("MyCustomProperty"). 我有一个Word文档,其中包含一个自定义属性(“ MyCustomProperty”)。 I would like to use C# to insert a DOCPROPERTY field with formatting and highlighting. 我想使用C#插入带有格式设置和突出显示的DOCPROPERTY字段。 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). 但是字体,大小和粗细都是默认值(Calibri(正文),11,不是粗体,不是斜体)。 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. 用对象模型编写Word文档对我来说不是很直观。 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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