代码之家  ›  专栏  ›  技术社区  ›  Lee.s

我想删除下划线。但是下面的代码太慢了

  •  2
  • Lee.s  · 技术社区  · 8 年前

    我想删除下划线(wdUnderlineWavy)。 但是下面的代码太慢了。角色越多,他们变得越慢。 有没有更快的办法?

        void remove_underline()
        {
    
            Word.Range range;
            for (int i = 1; i < Application.ActiveDocument.Characters.Count; i++)
            {
                range = Application.ActiveDocument.Characters[i];
    
    
                if (range.Font.Underline == Word.WdUnderline.wdUnderlineWavy
                       && (range.Font.UnderlineColor == (Word.WdColor)254 ||
                       range.Font.UnderlineColor == (Word.WdColor)32769 ||
                       range.Font.UnderlineColor == (Word.WdColor)16711681))
                {
                    range.Font.Underline = Word.WdUnderline.wdUnderlineNone;
                }
            }
        }
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   Lee.s    8 年前
        void remove_underline()
        {
                Word.Range range;
                range = Application.ActiveDocument.Content;
                remove_underline(range, 254);
                remove_underline(range, 32769);
                remove_underline(range, 16711681);
        }
        void remove_underline(Word.Range range, int colorIndex) {
            range.Find.ClearFormatting();
            range.Find.Replacement.ClearFormatting();
            range.Find.Text = "";
            range.Find.Font.UnderlineColor = (Word.WdColor)colorIndex;
            range.Find.Font.Underline = Word.WdUnderline.wdUnderlineWavy;
            range.Find.Replacement.Text = "";
            range.Find.Replacement.Font.Underline = Word.WdUnderline.wdUnderlineNone;
            range.Find.Execute(Format: true,Replace:Word.WdReplace.wdReplaceAll);
    
        }
    

    我解决了这个问题。 参考: https://social.msdn.microsoft.com/Forums/en-US/974a3a27-3ce9-43c6-9bbc-fbc39c6c0592/i-want-to-remove-the-underline-but-the-code-below-is-too-slow?forum=vsto