【问题标题】:Formatting text using word.interop in outlook 2013 add-in在 Outlook 2013 加载项中使用 word.interop 格式化文本
【发布时间】:2015-12-20 00:00:24
【问题描述】:

当用户按下功能区加载项中的按钮时,我正在尝试格式化我在新电子邮件开头插入的文本 代码很简单,它首先检查是否先前插入了文本类型,如果是则将其删除,然后使用 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);
    }

【问题讨论】:

    标签: c# office-interop outlook-addin


    【解决方案1】:

    解决了我遇到的问题。 我使用了以下内容:

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

    希望这可以帮助其他人。

    【讨论】:

      猜你喜欢
      • 2013-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-31
      • 2014-11-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多