C# 如何在word应用中将highlight ColorIndex属性重置为无突出显示

C# 如何在word应用中将highlight ColorIndex属性重置为无突出显示,c#,ms-word,C#,Ms Word,我在带有书签的word文档的标题中添加文本 然后我突出显示书签的文本 但如果在此之后我输入或键入另一些文本,这些文本也会突出显示。 我的代码是: Word.Document currDocument = WordApp.ActiveDocument; Word.Selection currentSelection = WordApp.Selection; if(currentSelection.HeaderFooter.IsHeader) { Word.Range selectionRa

我在带有书签的word文档的标题中添加文本

然后我突出显示书签的文本

但如果在此之后我输入或键入另一些文本,这些文本也会突出显示。

我的代码是:

Word.Document currDocument = WordApp.ActiveDocument;
Word.Selection currentSelection = WordApp.Selection;
if(currentSelection.HeaderFooter.IsHeader)
{
    Word.Range selectionRange = currentSelection.Range;

    selectionRange.Text ="abc";

    currentDocument.Bookmarks.Add("bookmark", selectionRange);
    currentDocument.Bookmarks[bookmarkName].Select();

    WordApp.Selection.Range.HighlightColorIndex = WdColorIndex.wdBrightGreen;

    **//from here I want to set highlight as off**
} 

我只想突出显示书签部分,而不是之后的部分。

使用
选择对象总是很棘手,如果可能的话应该避免。有时它会有所帮助,但在大多数情况下,使用
Range
对象更可靠<代码>选择
几乎反映了用户的工作方式。如果作为用户,您键入某个内容,选择该内容,应用高亮显示,然后再键入更多内容,您将看到所描述的行为。作为一个用户,您需要选择您键入的内容并删除突出显示-即使只有一两个字符。从那时起,高亮显示就消失了。无论是作为用户还是试图在代码中模仿它,这都是痛苦的

考虑问题中代码的以下变化。插入书签后,使用
replicate
属性将第二个
Range
对象设置为原始
Range
。(
Duplicate
很重要,因为否则两个
Range
对象将是相同的-更改一个对象也会更改另一个对象。)

第二个
范围
对象被移动到原始
范围
以外的位置。现在,这两个问题可以得到不同的处理。与
选择不同
代码可以处理许多
范围

Word.Document currDocument = WordApp.ActiveDocument;
Word.Selection currentSelection = WordApp.Selection;
if(currentSelection.HeaderFooter.IsHeader)
{
  Word.Range selectionRange = currentSelection.Range;
  selectionRange.Text ="abc";
  currentDocument.Bookmarks.Add("bookmark", selectionRange);
  //currentDocument.Bookmarks[bookmarkName].Select();
  Word.Range rngAfterBookmark = selectionRange.Duplicate;
  //go to the end of the bookmarked range
  rngAfterBookmark.Collapse(Word.WdCollapseDirection.wdCollapseEnd);
  //make sure the two ranges are no longer adjacent
  rngAfterBookmark.Text = " ";
  rngAfterBookmark.Collapse(Word.WdCollapseDirection.wdCollapseEnd);
  selectionRange.HighlightColorIndex = WdColorIndex.wdBrightGreen;
} 

注意:通常我甚至不会在页眉或页脚中使用
选择
,而是使用页眉或页脚的
范围
。我没有改变这一点,因为直到现在我还不知道代码的逻辑。

正如Cindy正确地提到的那样,应该尽可能避免使用选择。 也就是说,您需要将
wdNoHighLight
应用于非空范围才能生效。 因此,从最后一行开始,下面的代码就是这样做的。根据您的需要进行适当调整:

WordApp.Selection.Range.HighlightColorIndex = Word.WdColorIndex.wdBrightGreen;
WordApp.Selection.Collapse(Word.WdCollapseDirection.wdCollapseEnd);
WordApp.Selection.MoveRight(Word.WdUnits.wdCharacter, 1, Extend: true);
WordApp.Selection.Range.HighlightColorIndex = Word.WdColorIndex.wdNoHighlight;
WordApp.Selection.Collapse(Word.WdCollapseDirection.wdCollapseEnd);
WordApp.Selection.TypeText("YaddaYadda");
你应该看到没有突出显示的“yaddayada”

额外一点:使用COM对象时,双点(点选择点)通常会造成麻烦。尝试使用替代变量。还要确保使用

Marshal.ReleaseComObject(document);
并且在代码结束前释放所有其他单词引用

编辑:无选择的备选方案。最简单的方法是在非高亮显示的文本上简单地使用replace。为了简化,我访问了第一页的主标题

var section = currDocument.Sections.First;
var header = section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary];
var hRange = header.Range.FormattedText;
var highlightedText = "abc";
var normalText = " Yadda yadda";

//insert highlighted text and bookmark
hRange.Text = highlightedText;
currDocument.Bookmarks.Add("bookmark", hRange);
hRange.HighlightColorIndex = Word.WdColorIndex.wdBrightGreen;

//insert normal text, turn off highlighting
hRange.InsertAfter(normalText);
var find = hRange.Find;
find.ClearFormatting();
find.Replacement.ClearFormatting();
find.Text = normalText;
find.Replacement.Text = normalText;
find.Replacement.Highlight = (int) Word.WdColorIndex.wdNoHighlight;
find.Execute(Replace: Word.WdReplace.wdReplaceOne);

Marshal.ReleaseComObject(find);
Marshal.ReleaseComObject(hRange);
Marshal.ReleaseComObject(header);
Marshal.ReleaseComObject(section);

Word宏记录器为我提供了
wdNoHighlight
。在我的一个互操作应用程序中确认为
Word.WdColorIndex.wdNoHighlight
感谢您的快速响应。它帮助我解决了这个问题。我可以得到一个小例子,添加书签与页眉页脚范围按照您的方式,这样我就可以取代我的代码的选择过程enhancement@AmarMalik你能不能把这个问题作为一个新问题,像往常一样关注一下?这超出了原始问题的范围。。。但是有很多代码示例用于处理页眉/页脚“out there”。注意:当然,你也可以简单地改变这一点:首先用你想要的文本填充你的页眉,然后设置你的书签,此时不设置突出显示,然后快速替换要突出显示的文本,并用您最喜欢的突出显示颜色集替换它。。。