将格式化文本添加到现有书签

时间:2014-06-06 14:28:36

标签: c#-4.0 .net-4.0 visual-studio-2013 vsto word-2010

我正在尝试将一些格式化文本添加到现有书签中。由于某种原因,格式化不起作用,文本超出书签(书签右侧)而不是书签内。

这是我到目前为止的代码:

var bmkRange = this.bookmark1.Range;
object newRangeStart = bmkRange.Start;
object newRangeEnd = bmkRange.End;

Word.Range r = Range(ref newRangeStart , ref newRangeEnd);

// if ( a == b )
// {
r.Text = "some text here, ";
r.Bold = 0;
r.Font.Name = "Verdana";
r.Font.Size = 10.0F;
// }
// else if ( c == d )
// {
r.Text += "some more text here, ";
r.Bold = -1;
r.Font.Name = "Times New Roman";
r.Font.Size = 10.0F;
// }
// else if ( e == f )
// {
r.Text += "and even more text here.";
r.Bold = 0;
r.Font.Name = "Verdana";
r.Font.Size = 20.0F;
// }

我显然没有做到这一点。有人有什么想法吗?

这是使用visual studio 2013创建的word文档级项目。

1 个答案:

答案 0 :(得分:1)

您的代码存在两个问题。

  1. 文本插入书签后。
  2. 您的样式未正确应用。

  3. 问题#1和#2的解决方案:

    这主要是问题#2的解决方案。影响问题#1的部分在代码中被注释。

    介绍这个功能:

    /// <summary>
    /// Appends text to a range
    /// </summary>
    /// <param name="range">The range to insert into.</param>
    /// <param name="appendText">The text to append.</param>
    /// <param name="appendedRange">The range of the appended text</param>
    /// <returns>
    /// The range of the combined old text and the appended text
    /// </returns>
    private Word.Range AppendToRange(Word.Range range, string appendText, out Word.Range appendedRange)
    {
        // Fetch indexes
        object oldStartPosition = range.Start;           
        object oldEndPosition = range.End;
        object newEndPosition = (int)oldEndPosition + appendText.Length;
    
        // Append the text
        range.InsertAfter(appendText);
    
        // Define the range of the appended text
        appendedRange = Range(ref oldEndPosition, ref newEndPosition);
    
        // Return the range of the new combined range
        return Range(ref oldStartPosition, ref newEndPosition);
    }
    

    并将您的代码更新为以下内容:

    var bookmark = this.bookmark1;
    // Solution to issue #1: Keep the name of the bookmark
    var bookmarkName = bookmark.Name; 
    var bmkRange = bookmark.Range;
    
    Word.Range r;
    
    // if ( a == b )
    // {
    bmkRange = AppendToRange(bmkRange, "some text here, ", out r);
    r.Bold = 0;
    r.Font.Name = "Verdana";
    r.Font.Size = 10.0F;
    // }
    // else if ( c == d )
    // {
    bmkRange = AppendToRange(bmkRange, "some more text here, ", out r);
    r.Bold = -1;
    r.Font.Name = "Times New Roman";
    r.Font.Size = 10.0F;
    // }
    // else if ( e == f )
    // {
    bmkRange = AppendToRange(bmkRange, "and even more text here.", out r);
    r.Bold = 0;
    r.Font.Name = "Verdana";
    r.Font.Size = 20.0F;
    // } 
    
    // Solution to issue #1: Restore the bookmark
    object newBmkRange = bmkRange;
    Bookmarks.Add(bookmarkName, ref newBmkRange);
    

    问题解决方案的基本原理#1: 当您附加书签时,Word将在书签后插入文本。因此,在完成更新后,您需要恢复书签以覆盖整个范围。这是通过跟踪书签名称然后覆盖它(有效地更新它)来完成的。

    问题解决方案的基本原理#2: 在您的代码示例中,您将反复设置整个范围的样式。您需要提取范围的附加文本和样式的范围。这可以通过提供的函数的out参数来解决。