Search code examples
c#office-interopoutlook-addin

Formatting text using word.interop in outlook 2013 add-in


i'm trying to format text that i'm inserting at the beginning of a new email when a user presses a button in the ribbon add-in the code is simple in that it first checks if the type of text was previously inserted, removes it if it was, and then inserts a new bracket of text using Word.Range.InsertBefore(.....); the text will be: CLASSIFICATION: ....... CLASSIFICATION: .......

the problem that i am running into is after the insert i'm formatting the font using Range.Font,Text etc... i need the CLASSIFICATION: ...... formatted and not the space between them

the space between the "CLASSIFICATION......" is being formatted when typing begins to the same size, color, etc.... as the "CLASSIFICATION"

the code i'm using is:

        private void setClassificationText(string classificationText, Word.WdColorIndex color)
    {
        //current mail item document
        Word.Document document = (Word.Document)mailItem.GetInspector.WordEditor;           
        Word.Range rng = document.Paragraphs[1].Range;
        //check if a classification level was already supplied
        if (checkIfClassificationExists(rng))
        {
            //get the first 2 Paragraphs and remove the text from them
            int paraCount = 2;
            for (int i = 1; i <= paraCount; i++)
            {
                Word.Range classificationRange = document.Paragraphs[i].Range;
                removeTextFromParagraph(classificationRange);
            }

            rng.Collapse();
            rng = document.Paragraphs[1].Range;         
        }

        rng.InsertBefore(classificationText + CT.lineFeedStr5 + CT.classification + classificationText + CT.lineFeedStr3);      

        //sets the color and text
        rng.Font.ColorIndex = color;
        rng.Text = CT.classification + rng.Text;
        rng.Font.Bold = 1;
    }


        private void removeTextFromParagraph(Word.Range rng)
    {
        int wordCount = rng.Words.Count;
        rng.Delete(Count: wordCount);
    }

Solution

  • solved the issue i was having. i used the following:

    private void setClassificationText(string classificationText, Word.WdColorIndex color)
        {
            //current mail item document
            Word.Document document = (Word.Document)mailItem.GetInspector.WordEditor;
            Word.Range rng = document.Paragraphs[1].Range;
            //check if a classification level was already supplied
            if (checkIfClassificationExists(rng))
            {
                //get the first 2 Paragraphs and remove the text from them
                int paraCount = 2;
                for (int i = 1; i <= paraCount; i++)
                {
                    Word.Range classificationRange = document.Paragraphs[i].Range;
                    removeTextFromParagraph(classificationRange);
                }
    
                rng.Collapse();
                rng = document.Paragraphs[1].Range;
            }
    
            //insert the text
            rng.InsertBefore(classificationText + CT.lineFeedStr5 + CT.classification + classificationText);        
    
            //sets the color and text
            rng.Font.ColorIndex = color;
            rng.Text = CT.classification + rng.Text;
            rng.Font.Bold = 1;
    
            //get the beginning and ending positions of the blank space
            startPosition = rng.Start + getClassificationLength(classificationText);
            endPosition = (int)startPosition + CT.lineFeedStr5.Length-1;
    
            //get the Word.Range for the blank space
            Word.Range rngNormal = document.Range(ref startPosition, endPosition);
            //set blank space to be normal font (not bold and black)
            rngNormal.Bold = 0;
            rngNormal.Font.ColorIndex = Word.WdColorIndex.wdBlack;
        }
    
    private int getClassificationLength(string classificationText)
        {
            int returnValue = 0;
            returnValue = CT.classification.Length + classificationText.Length;
            return returnValue;
        }
    

    hope this can help someone else out.