Search code examples
c#office-interopnovacode-docx

How to change background color with DocX (Microsoft Word) documents


I am trying to change the background color (only for a few words) in a Word document.

I am using DocX (https://docx.codeplex.com/SourceControl/latest#Examples/Program.cs), but I cannot find any methods/properties which allow me to do so.

Here is my code:

    var rb = new Formatting();
        rb.UnderlineColor = this.GetResultColor(stu.RespectfulBehavior);
        rb.FontColor = this.GetResultColor(stu.RespectfulBehavior);

templ.ReplaceText(PlaceHolders.RespectfulBehaviour.GetDisplayName(), "    ", newFormatting: rb);

Solution

  • Looking at the DocX source code of the Formatting in class I would say that you're looking for Highlight. So if you wanted something highlighted in blue

    rb.Highlight = Highlight.blue;
    

    Edit: This seems to have been already answered in this question https://stackoverflow.com/a/30141775/2039359