Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9063697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:04:16+00:00 2026-06-16T16:04:16+00:00

Using the Word Interop API I have a document from which I remove text

  • 0

Using the Word Interop API I have a document from which I remove text that have been highlighted with a specific color. Boiled down, the logic is similar to this:

if(range.HighlightColorIndex == WdColorIndex.wdYellow)
{
    range.Delete();
}

For some documents I have noticed that range.HighlightColorIndex returns the value: 999999.

By taking a look the WdColorIndex enum (which is the type of the HighlightColorIndex property), I see the enum is implemented with elements assigned a value in the interval from -1 to 16, which doesn’t explain the 999999 number returned.

Further, by using Word, I have noticed some odd behavior. Creating a new document with two yellow-highlighted lines – the first containing text and a hyperlink and the second containing just text:

  • Before the document has been saved and closed for the first time, Word do recognize each line as being highlighted with a yellow color when selecting them one-by-one.
  • After the document has been saved, closed, and re-opened, Word no longer recognizes the line with text and a hyperlink as being highlighted, whereas the line with only text is still recognized as being highlighted.

A screenshot of Word’s highlight recognition after opening the document is seen here:

Possible Word Highlight Bug

From this study, it seems that this might be a bug in Word, but I want to be sure that I’m not missing something here – so, basically, does anybody know if this is intended behavior or a bug, and further, what a reasonable approach would be to deal with it when using Word Interop in the given case?

For this scenario, I’m running Office 2010 and version 14 of the Word Interop API.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T16:04:17+00:00Added an answer on June 16, 2026 at 4:04 pm

    The value of 999999 is used by Word to indicate that the current selection contains more than one of the respective formatting.

    In your example, the hyperlink is represented as a Word field (press Alt-F9 to toggle the code view) which contains a highlighted run. The field itself does not have highlighting applied after the document is re-opened. This is not necessarily a bug in Word, it’s just the behavior of (hyperlink) fields not to remember the formatting on the field level as such.

    However, your actual task seems to be to remove highlighted text from a document. This can usually be better done using the Range.Find object of Word:

    range.Find.ClearFormatting();
    range.Find.ClearAllFuzzyOptions();
    range.Find.Highlight = 1;
    range.Find.Replacement.ClearFormatting();
    range.Find.Replacement.Text = "";
    range.Find.Wrap = Word.WdFindWrap.wdFindContinue;
    range.Find.Execute(Replace: Word.WdReplace.wdReplaceAll);
    

    Here is a complete sample program that you can use to remove highlight:

    using System;
    using System.Linq;
    using Word = Microsoft.Office.Interop.Word;
    
    
    class Program
    {
        static void Main(string[] args)
        {
            var fileName = args[0];
    
            var wordApp = new Word.Application();
            wordApp.Visible = true;
            var document = wordApp.Documents.Open(fileName);
            RemoveHighlightingEverywhere(document);
        }
    
        static void RemoveHighlightingEverywhere(Word.Document document)
        {
            foreach (Word.Range storyRange in document.StoryRanges)
            {
                var range = storyRange;
                while (range != null)
                {
                    RemoveHighlightingFromRange(range);
    
                    if (range.ShapeRange.Count > 0)
                    {
                        foreach (Word.Shape shape in range.ShapeRange)
                        {
                            if (shape.TextFrame.HasText != 0)
                            {
                                RemoveHighlightingFromRange(
                                    shape.TextFrame.TextRange);
                            }
                        }
                    }
                    range = range.NextStoryRange;
                }
            }
        }
    
        static void RemoveHighlightingFromRange(Word.Range range)
        {
            range.Find.ClearFormatting();
            range.Find.ClearAllFuzzyOptions();
            range.Find.Highlight = 1;
            range.Find.Replacement.ClearFormatting();
            range.Find.Replacement.Text = "";
            range.Find.Wrap = Word.WdFindWrap.wdFindContinue;
            range.Find.Execute(Replace: Word.WdReplace.wdReplaceAll);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am iterating through paragraphs in a word document using word interop API. So
I have a windows service written in c# which reads the text from word
I'm a learner in using Microsoft.Office.Interop.Word . Consider I have a word document with
I Opened/Created a new Word document dynamically on FormLoad using: using Word = Microsoft.Office.Interop.Word;
I'm trying to add some HTML formatted text to Word using Office Interop. My
I want to detect Empty paragraphs in Word Document using Microsoft.Office.Interop.Word. Suppose, if my
I am actually using the library DocX to generate Word document (2007+) from .Net.
I am using Interop.Microsoft.Office.Interop.Word.dll to dynamically build a Word document in C#. Does anyone
Background : I am trying to generate a word document using c# and Interop.
I'm creating a Word 2010 document using C# and Microsoft.Office.Interop.Word . Using the Range.Paste

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.