簡體   English   中英

在Outlook 2013加載項中使用word.interop格式化文本

[英]Formatting text using word.interop in outlook 2013 add-in

當用戶按下功能區加載項中的按鈕時,我試圖格式化要在新電子郵件開頭插入的文本,該代碼很簡單,因為它首先檢查先前是否插入了文本類型,如果是的話,然后使用Word.Range.InsertBefore(.....)插入新的文本括號; 文本為:分類:.......分類:.......

我遇到的問題是插入后,我正在使用Range.Font,Text等對字體進行格式化...我需要分類:......格式化,而不是它們之間的空格

當鍵入開始與“ CLASSIFICATION”具有相同的大小,顏色等時,“ CLASSIFICATION ......”之間的空格將被格式化。

我使用的代碼是:

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

解決了我遇到的問題。 我使用以下內容:

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

希望這可以幫助別人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM