我有一个带有电子邮件模块的Windows应用程序。该表单包含一个RTF控件,客户端可以添加任何Rich Text,然后将其转换为HTML并在发送电子邮件时将其设置为电子邮件正文。问题是它一直在提示在发送电子邮件之前保存文档,我正在尝试在幕后执行此操作,以便不会将word文档保存到用户计算机。这是进行转换的代码:
private string RtfToHtml(string strRTF)
{
//Declare a Word Application Object and a Word WdSaveOptions object
Microsoft.Office.Interop.Word.Application myWord;
Object oDoNotSaveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
//Declare two strings to handle the data
string sReturnString = string.Empty;
string sConvertedString = string.Empty;
try
{
//Instantiate the Word application,
//set visible to false and create a document
myWord = new Microsoft.Office.Interop.Word.Application();
myWord.Visible = false;
myWord.Documents.Add();
// Create a DataObject to hold the Rich Text
//and copy it to the clipboard
System.Windows.Forms.DataObject doRTF = new System.Windows.Forms.DataObject();
doRTF.SetData("Rich Text Format", strRTF);
Clipboard.SetDataObject(doRTF);
// 'Paste the contents of the clipboard to the empty,
//'hidden Word Document
myWord.Windows[1].Selection.Paste();
// '…then, select the entire contents of the document
//'and copy back to the clipboard
myWord.Windows[1].Selection.WholeStory();
myWord.Windows[1].Selection.Copy();
// 'Now retrieve the HTML property of the DataObject
//'stored on the clipboard
sConvertedString = Clipboard.GetData(System.Windows.Forms.DataFormats.Html).ToString();
// Remove some leading text that shows up in some instances
// '(like when you insert it into an email in Outlook
sConvertedString = sConvertedString.Substring(sConvertedString.IndexOf("<html"));
// 'Also remove multiple  characters that somehow end up in there
sConvertedString = sConvertedString.Replace("Â", "");
//'…and you're done.
sReturnString = sConvertedString;
if (myWord != null)
{
myWord.Documents[1].Close(oDoNotSaveChanges);
((Word._Application)myWord).Quit(oDoNotSaveChanges);
myWord = null;
}
}
catch (Exception ex)
{
MessageBox.Show("Error converting Rich Text to HTML: " + ex.Message);
}
return sReturnString;
}
...这会提示用户保存word文档...但它在我的开发环境中工作正常。有没有人知道为什么会这样? 感谢
答案 0 :(得分:0)
看起来我在CodeProject上找到了另一个似乎可以解决问题的解决方案。 http://www.codeproject.com/Tips/493346/RTF-TO-HTML