2011-02-10 18 views
2

我試圖將一個字符串附加到一個傳出的Outlook.MailItem。在發送事件處理程序中,我有:用C編輯Outlook.MailItem的RTFBody#

switch (mailItem.BodyFormat) { 
    case Outlook.OlBodyFormat.olFormatRichText: 
     byte[] mailItemBytes = mailItem.RTFBody as byte[]; 
     System.Text.Encoding encoding = new System.Text.ASCIIEncoding(); 
     string RTF = encoding.GetString(mailItemBytes); 
     RTF += "my string"; 
     byte[] moreMailItemBytes = encoding.GetBytes(RTF); 
     mailItem.RTFBody = moreMailItemBytes; 
     break; 
    // ... 
} 

但收到的電子郵件中不包含我的字符串。

回答

0

RTF是文件相當複雜的格式,並不會像連接字符串一樣簡單。您可以嘗試使用RichTextBox控件並首先在其中導入數據,向其添加文本,然後重新獲取格式化值。有點笨重,但比解析RTF文件要容易得多。

作爲替代方案,你可以找到一個分析,並與RTF的作品庫,但是這意味着你的應用程序,最有可能另一個DLL的依賴中釋放包括。

0

我知道這是舊的,已經綠色檢查,但搜索類似的問題後,我發現一個頁面,提供了一個很好的答案,如何使用Word.Document對象模型修改Outlook項目中的RTF正文。 https://www.add-in-express.com/forum/read.php?FID=5&TID=12738

基本上你把文本中的Word文檔,而忘記了與RTF的工作都在一起。您需要首先將Microsoft.Office.Interop.Word的引用添加到您的項目中。

然後使用添加到您的項目

using Word = Microsoft.Office.Interop.Word; 

然後添加代碼

Word.Document doc = Inspector.WordEditor as Word.Document; 

//text body 
string text = doc.Content.Text; 

//end of file 
int endOfFile = (text.Length) > 0 ? text.Length - 1 : 0; 

//Select the point to add or modify text 
Word.Range myRange = doc.Range(endOfFile, endOfFile); 

//add your text to end of file 
myRange.InsertAfter("my string");