簡體   English   中英

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

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

我有一個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,不是粗體,不是斜體)。 文本未突出顯示。

我想要的是將字段本身與我配置的字體,大小和粗細添加到一行中,並右對齊。

我究竟做錯了什么?

用對象模型編寫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);

如果您有更好的建議,我會全力以赴。

我遇到了同樣的問題,您的答案是正確的,但是另一種解決方案是僅在添加字段之后設置屬性,但是要在原始范圍上設置(或者像執行操作一樣再次獲取引用)

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