使用 C# 编辑 Outlook.MailItem 的 RTFBody

发布于 2024-10-16 15:23:39 字数 534 浏览 5 评论 0原文

我正在尝试将字符串附加到传出的 Outlook.MailItem。在发送事件处理程序中,我有:

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

但收到的电子邮件不包含我的字符串。

I'm trying to append a string to an outgoing Outlook.MailItem. In the send event handler I have:

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

but the received email does not contain my string.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

几味少女 2024-10-23 15:23:39

我知道这是旧的并且已经有绿色复选标记,但在搜索类似问题后,我发现一个页面对于如何使用 Word.Document 对象模型修改 Outlook 项目中的 RTF 正文提供了很好的答案。 https://www.add-in- express.com/forum/read.php?FID=5&TID=12738

基本上,您将文本视为 Word 文档,而忘记了使用 RTF。您需要首先将 Microsoft.Office.Interop.Word 的引用添加到您的项目中。

然后将 using 添加到您的项目中

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

I know this is old and has green check already but after searching for similar issues I found a page that gives a good answer for how to modify the RTF body in an outlook project using the Word.Document object model. https://www.add-in-express.com/forum/read.php?FID=5&TID=12738

Basically you treat the text a word doc and forget about working with RTF all together. You will need to add reference of Microsoft.Office.Interop.Word to your project first.

then add using to your project

using Word = Microsoft.Office.Interop.Word;

then add your code

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");
乙白 2024-10-23 15:23:39

RTF 是一种非常复杂的格式文件,并且不会像连接字符串那么容易。您可以尝试使用 RichTextBox 控件并导入首先获取那里的数据,向其中添加文本,然后重新获取格式化的值。有点笨重,但比解析 RTF 文件容易得多。

作为替代方案,您也许能够找到一个解析并使用 RTF 的库,但这意味着您的应用程序的依赖项,并且很可能会在发行版中包含另一个 DLL。

RTF is a pretty elaborate format for files and isn't going to be as easy as concatenating a string. You may try using the RichTextBox control and importing the data there first, adding text to it, then re-grabbing the formatted value back. A bit clunky, but a lot easier than parsing an RTF file.

As an alternative, you may be able to find a library that parses and works with RTF, but that means a dependency for your application and most likely another DLL to include in the release.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文