【问题标题】:How can I add underlined words in a line of text in a word header object?如何在单词标题对象的一行文本中添加带下划线的单词?
【发布时间】:2019-03-09 07:06:59
【问题描述】:

我需要在文档的标题对象中添加带下划线的列标题。我正在使用 C# 和 Microsoft.Office.Interop.Word

代码的相关部分如下所示...

foreach (Word.HeaderFooter header in wordSection.Headers)
{
    int[] fiscalYears = RetrieveFiscalYears(docProfile);
    string paddingFY = new String(' ', 8);
    Word.Paragraph colParagraph = wordRng.Paragraphs.Add();

    int year;
    for (int i = fiscalYears.Length - 1; i >= 0; i--)
    {
        year = fiscalYears[i];
        colParagraph.Range.InsertAfter(MARKUP_WORD_TAB);
        //begin underline
        colParagraph.Range.InsertAfter(paddingFY + year.ToString() + paddingFY);
        //end underline
    }
    colParagraph.Range.InsertAfter(MARKUP_WORD_TAB);
    colParagraph = wordRng.Paragraphs.Add();
    colParagraph.set_Style(wordDoc.Styles["ColumnHeadings"]);
}

基本上它需要看起来类似于...

 Expended          Estimated          Budgeted
   2015               2016              2017
 ---------         ----------         --------

在文档的正文中,我的 for 循环看起来像

foreach (int year in fiscalYears)
{
    wordApp.Selection.TypeText(MARKUP_WORD_TAB);
    wordApp.Selection.Font.Underline = Word.WdUnderline.wdUnderlineSingle;
    wordApp.Selection.TypeText(paddingFY + year.ToString() + paddingFY);
    wordApp.Selection.Font.Underline = Word.WdUnderline.wdUnderlineNone;
}

但是当我使用选择对象时,它会写入文档的正文,而不是页眉/页脚对象。我也许可以通过使用 SeekHeader 并使其成为焦点来解决这个问题,但这也带来了自己的挑战...... 我尝试使用 colParagraph.Range.Font.Underline 对象,但它强调了整行,而不仅仅是构成列标题的单词。 我尝试使用查找对象,但由于某种原因,执行找不到文本。

感谢您提供的任何指导。

【问题讨论】:

  • 当然可以,我只是不确定将这些信息放在页眉中是否是最好的方法......?您不使用带有重复标题行的表格是否有原因?
  • 嗯,第一个原因是我正在修改已经编写了一个包含几个高度格式化部分的非常大的文档的代码,我只需要向第二页标题对象添加标题(第一页标题是空白的,因为的连续部分)。 Word(和 Excel)都不能很好地处理单元格中的大量格式化文本。

标签: c# ms-word underline


【解决方案1】:

我不得不将设置样式移到 for 循环上方,并根据段落范围设置一个新范围,并移动它的开始和结束位置。然后将下划线应用于新范围。 所以现在它看起来类似于 ....

colParagraph.Range.InsertAfter(MARKUP_WORD_TAB);
colParagraph = wordRng.Paragraphs.Last;         //reset the range to include the tab so the style can be applied.
colParagraph.set_Style(wordDoc.Styles["ColumnHeadings"]);

int year;
int start = colParagraph.Range.Text.Length - 1;
string yrHeading = string.Empty;
Word.Range underlineRange = null;
for (int i = 0 ; i < fiscalYears.Length; i++)
{
    year = fiscalYears[i];
    colParagraph = wordRng.Paragraphs.Last;         //reset the range to include the last fiscal year that was entered.
    start = colParagraph.Range.Text.Length - 1;

    colParagraph.Range.InsertAfter(yrHeading);
    colParagraph.Range.InsertAfter(MARKUP_WORD_TAB);
    underlineRange = colParagraph.Range.Duplicate;
    underlineRange.MoveStart(Word.WdUnits.wdCharacter, start);
    underlineRange.MoveEnd(Word.WdUnits.wdCharacter, -2);           //-2 = /t/r for tab & paragraph characters
    underlineRange.Font.Underline = Word.WdUnderline.wdUnderlineSingle;
}
colParagraph = wordRng.Paragraphs.Add();

【讨论】:

    猜你喜欢
    • 2017-01-05
    • 2013-04-29
    • 2017-12-04
    • 2012-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-05
    相关资源
    最近更新 更多