tags:

views:

91

answers:

1

I've added functionality to an application that prints out a bunch of information to a FixedDOcument and sends this off to the printer. This works just fine, however the request is that there be an in application function that emails the document using OUtlook and its here that I come unstuck. I'd very much like to just reuse the class that makes the fixed document for printing to generate the text for email, but I'm struggling to do this.

I've tried the following...

Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
MailItem email = (MailItem)(oApp.CreateItem(OlItemType.olMailItem));
email.Recipients.Add("[email protected]");
email.Subject = "Hello";
email.Body = "TEST";
FixedDocument doc = CreateReport(); //make my fixed document

//this doesn't work, and the parameters it takes suggest it never will
email.Attachments.Add(doc, OlAttachmentType.olByValue, 1, null); 
email.Send();

I can't help but think I'm on completely the wrong tack here, but I don't really want to have to write a bunch of new text formatting (since email.Body only takes a string) when I've already got the content formatted how I want it.

Note that the content is all textual, so I don't really care if it gets sent as an attachment or as text in the emails body. Ideally if its sent as an attachment it won't be saved anywhere permanently.

Any pointers?

+1  A: 

Save the Fixed document to the users temporary folder, than put the full path name of the document in the attachment, send it, and then delete the document from temporary folder.

Attachment Signature

Attachment Add (
    [InAttribute] Object Source,
    [OptionalAttribute] [InAttribute] Object Type,
    [OptionalAttribute] [InAttribute] Object Position,
    [OptionalAttribute] [InAttribute] Object DisplayName
)

Source Parameter:

Source
The source of the attachment. This can be a file (represented by the full file system path with a file name) or an Outlook item that constitutes the attachment.

Saving Document:

String TempFoder = System.Environment.GetEnvironmentVariable("TEMP");
String tempFileName = TempFoder + @"\TempFixedDocument.doc";
FixedDocument.Save(tempFileName);

Sending Email:

Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
MailItem email = (MailItem)(oApp.CreateItem(OlItemType.olMailItem));
email.Recipients.Add("[email protected]");
email.Subject = "Hello";
email.Body = "TEST";
FixedDocument doc = CreateReport(); //make my fixed document

//this doesn't work, and the parameters it takes suggest it never will
email.Attachments.Add(tempFileName, OlAttachmentType.olByValue, 1, null); 
email.Send();

Deleting Temp File:

System.IO.File.Delete(tempFileName);
LnDCobra
FixedDocument doesn't have a save() method, so I cant do that. I'm going to try writing it to an XPS Document (which I *can* save) and emailing that.
MoominTroll
Worked just fine, cheers. Used XpsDocumentWriter to write and save to an XPS file (obviously changed "FixedDocument.doc" to .xps) but the core of this method will work for anyone trying to write to any format.
MoominTroll