2013-03-05 72 views
2

我使用UTF8Encoding從MailItem編碼RTFbody已經取得了一些成功。我可以編寫一封新郵件,完成所有新郵件,然後點擊發送。點擊發送後,我會在電子郵件中添加一個也添加到類別中的標籤。這一切都可以通過RTFBbody工作。通過編碼編輯MailItem.RTFBody

問題出現在我回復RTF電子郵件時,出於測試目的,這些電子郵件只是我發給我孤獨自己的電子郵件。當我發送回覆郵件並添加新標籤時,我先刪除舊標籤,然後添加新標籤。當我使用包含新標籤的編輯字符串在回覆電子郵件中設置RTFBody時,出現「內存不足或磁盤空間不足」錯誤。這不會發生,當我只是刪除具有相同功能的標籤。

貝婁是我使用的代碼:

private void ChangeRTFBody(string replaceThis, string replaceWith) 
{ 
    byte[] rtfBytes = Globals.ThisAddIn.email.RTFBody as byte[]; 
    System.Text.Encoding encoding = new System.Text.UTF8Encoding(); 
    string rtfString = encoding.GetString(rtfBytes); 

    rtfString = rtfString.Replace(replaceThis, replaceWith); 

    rtfBytes = encoding.GetBytes(rtfString); 
    Globals.ThisAddIn.email.RTFBody = rtfBytes; < // The error is here only on 
                // reply and only when I replace 
                // with new tags 
} 

這些都是我打的電話:

刪除舊標籤:ChangeRTFBody(lastTag, "");

添加新的標籤:ChangeRTFBody("}}\0", newTag + "}}\0");

像我說過,當我創建一封新電子郵件併發送時,它會起作用,但當我嘗試回覆同一封電子郵件時,則不起作用。似乎byte[]的大小在刪除後幾乎翻倍。當我在刪除過程中檢查它時,它的大小約爲15k字節,當我在「添加」過程中檢查時,它跳到了超過30k字節。當我嘗試將新增充值的byte[]添加到rtfBody時出現錯誤。

感謝您的任何幫助和提示,並對所有閱讀感到抱歉。

回答

1

我有同樣的問題,並且遇到了我認爲通過使用Word.Document對象模型來替換Outlook rtf正文中的文本的更簡單的方法。您需要首先將Microsoft.Office.Interop.Word的引用添加到您的項目中。

然後添加使用

using Word = Microsoft.Office.Interop.Word; 

那麼你的代碼看起來像

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

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

//find text location 
int textLocation = text.IndexOf(replaceThis); 

if(textLocation > -1){ 
    //get range 
    int textLocationEnd = textLocation + replaceThis.Length; 

    //init range 
    Word.Range myRange = doc.Range(textLocation , textLocationEnd); 

    //replace text 
    myRange.Text = replaceWith 
} 
+0

是的,我結束了使用WordEditor爲好。編輯RTF讓人頭疼。 – Natzely 2017-05-02 15:05:45