如何在word应用程序中将高亮显示ColorIndex属性重置为无高亮显示



我正在一个带书签的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**
} 

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

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

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

该第二CCD_ 9对象被移动到原始CCD_。现在可以用不同的方式处理这两者。与Selection不同,代码可以与许多Ranges一起工作。

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;
} 

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

正如Cindy正确提到的,应尽可能避免使用Selection。也就是说,您需要将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");

您应该看到没有高亮显示的"YaddaYadda"。

还有一点:使用COM对象时,双点(点选择点)通常会引起麻烦。请尝试使用替代变量。还要确保使用

Marshal.ReleaseComObject(document);

并在代码结束前发布所有其他Word引用。

编辑:不带选择的备选方案。最简单的方法是简单地对未高亮显示的文本使用替换。为了简化,我访问了第一页的主要标题。

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);

最新更新