Jaster
Jaster

Reputation: 8581

Copy Word format into Outlook message

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?

Upvotes: 1

Views: 3164

Answers (2)

Jaster
Jaster

Reputation: 8581

Office 2007 offers "MailEnvelope" feature, that can be used for my purpose.

Upvotes: 0

Ta01
Ta01

Reputation: 31610

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.

Upvotes: 1

Related Questions