0

I have a Windows application that has an Email module. The form contains an RTF control where the client can add any Rich Text and I convert that to HTML and set it as the email body when I send an email. The problem is that it has been prompting to save the document before sending the email and I am trying to do this behind the scenes so that no word document is saved to the users machine. This is the code that does the converting:

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

...and this prompts the user to save a word document...but it works fine in my development environment. Does anyone have any idea of why this would happen? Thanks

devguy
  • 37
  • 1
  • 9

1 Answers1

0

Looks like I found another solution on CodeProject that seems to do the trick. http://www.codeproject.com/Tips/493346/RTF-TO-HTML

devguy
  • 37
  • 1
  • 9