将 Word 格式复制到 Outlook 邮件中
我有一个 Outlook 自动化。我想使用Word文档作为消息内容的模板。假设我有一些包含表格、颜色、大小等的格式化文本。现在我想将此内容复制/粘贴到 Outlook 消息对象中。
以下是一些示例代码(无需清理):
String path = @"file.docx";
String savePath = @"file.msg";
Word.Application wordApp = new Word.Application();
Word.Document currentDoc = wordApp.Documents.Open(path);
Word.Range range = currentDoc.Range(0, m_CurrentDoc.Characters.Count);
String wordText = range.Text;
oApp = new Outlook.Application();
Outlook.NameSpace ns = oApp.GetNamespace("MAPI");
ns.Logon("MailBox");
Outlook._MailItem oMsg = oApp.CreateItem(Outlook.OlItemType.olMailItem);
oMsg.To = "[email protected]";
oMsg.Body = wordtext;
oMsg.SaveAs(savePath);
使用 Outlook/Word 2007 年,但 Word 文件仍可能采用 2000/2003 格式 (.doc)。
Visual Studio 2010 与 .net 4.0(由于示例代码,应该很明显)。
我习惯了互操作,我知道我目前只是复制“纯文本”。我认为必须通过从 word 文档中检索 rtf/html 来完成...
有什么建议吗?
I have an outlook automation. I would like to use a Word document as template for the message content. Lets say i have some formatted text containing tables, colors, sizes, etc. Now I'd like to copy/paste this content into an Outlook message object.
Here is some Sample Code (no cleanup):
String path = @"file.docx";
String savePath = @"file.msg";
Word.Application wordApp = new Word.Application();
Word.Document currentDoc = wordApp.Documents.Open(path);
Word.Range range = currentDoc.Range(0, m_CurrentDoc.Characters.Count);
String wordText = range.Text;
oApp = new Outlook.Application();
Outlook.NameSpace ns = oApp.GetNamespace("MAPI");
ns.Logon("MailBox");
Outlook._MailItem oMsg = oApp.CreateItem(Outlook.OlItemType.olMailItem);
oMsg.To = "[email protected]";
oMsg.Body = wordtext;
oMsg.SaveAs(savePath);
Using Outlook/Word 2007, however the word files still mayb in 2000/2003 format (.doc).
Visual Studio 2010 with .net 4.0 (should obvious due to the samplecode).
I'm used to interop and I know that I am currently just copieing the "plain text". I think it has to be done via retreiving rtf/html from the word document...
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在读取内容的字符串将无法保持格式。您需要通过其他方式复制所选内容以保留格式。
您可以使用的一种技术是将文档保存为 html 格式 - 这将添加额外的 html 单词倾向于添加,将 html 内容读入字符串,即该字符串将具有生成的标签 - ,然后创建一个 Outlook mailItem 和将 正文格式 设置为 html。 - 并将正文设置为将单词保存在生成的html中的html。
The string you are reading the contents into is not going to be able to maintain the format. You will need to copy the selection by alternative means to retain the formatting.
One technique you can use is to save the document in html format - which will add the extra html word tends to add, read the html content into a string, i.e. this string will have the generated tags - , and then create an outlook mailItem and set the body format to html. - and set the body to the html that saving the word in html generated.
Office 2007 提供了“MailEnvelope”功能,可以用于我的目的。
Office 2007 offers "MailEnvelope" feature, that can be used for my purpose.