Hi have this code that runs well for me. It's part of my WordWrapper where Sel is defined as the Selection object.
I tried to save Sel.Font to a var f variable, and reset Sel.Font = fbefore exit, but that does not work. "Value out of range".

        /// <summary>
        /// Write String Bold/Underline/Size 14.
        /// </summary>
        /// <param name="s"></param>
        public void Header(string s)
        {
            var b = Sel.Font.Bold;
            var u = Sel.Font.Underline;
            var size = Sel.Font.Size;
            Sel.Font.Bold = (int) Word.WdConstants.wdToggle;
            Sel.Font.Underline = Word.WdUnderline.wdUnderlineSingle;
            Sel.Font.Size = 14;
            Sel.TypeText(s);
            Sel.Font.Bold = b;
            Sel.Font.Underline = u;
            Sel.Font.Size = size;
        }

Dani AI

Generated

— short answer: you can't copy/assign the COM Font object (that's why Sel.Font = f gave "Value out of range"). Font is a COM interface, not a simple value, so you must either (a) set the individual Font properties you care about, (b) apply a style, or (c) format only the Range that contains the new text instead of changing the Selection and trying to restore its Font object.

A small, robust pattern that avoids changing the caller's Selection formatting is: capture the insertion start, insert the text, grab a Range for just the new text, and format that Range. That keeps changes local and doesn't require restoring the whole Selection state.

public void Header(string text)
{
    int start = Sel.Range.Start;
    Sel.TypeText(text);
    int end = Sel.Range.End;
    var rng = Sel.Document.Range(start, end);            // range for inserted text
    rng.Font.Bold = -1;                                  // use -1 for True in Word COM
    rng.Font.Underline = Word.WdUnderline.wdUnderlineSingle;
    rng.Font.Size = 14f;
    rng.Collapse(Word.WdCollapseDirection.wdCollapseEnd);
    Sel.SetRange(rng.End, rng.End);                      // restore selection to after text
    Sel.TypeParagraph();
}

If you're producing real "headers," using a built-in or custom paragraph style (as you started doing) is cleaner long-term: styles survive edits, respect numbering/TOC, and remove the need to restore formatting. If you must temporarily change Selection formatting, save individual property values as object old = Sel.Font.Bold; and restore them in a finally block to avoid leaving Word in an inconsistent state. Also consider using Range.InsertAfter instead of Selection where possible and disable screen updates during batch changes to avoid flicker.

This looks slightly better and uses built in styles.

       public void Header(string s)
        {
            Sel.TypeText(s);
            var p = Sel.Paragraphs.Last;
            p.set_Style(Word.WdBuiltinStyle.wdStyleHeading1);
            Sel.TypeParagraph();          
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.