Convert RTF to Html 提示保存一个word文档

Convert RTF to Html prompts to save a word document

我有一个具有电子邮件模块的 Windows 应用程序。该表单包含一个 RTF 控件,客户端可以在其中添加任何富文本,我将其转换为 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 文档...但它在我的开发环境中运行良好。有谁知道为什么会发生这种情况? 谢谢

看来我在 CodeProject 上找到了另一个似乎可以解决问题的解决方案。 http://www.codeproject.com/Tips/493346/RTF-TO-HTML