none
How to attach an unsaved Word document to Outlook email programmatically RRS feed

  • Question

  • I am trying to use  MailItem.Attachments.Add ActiveDocument.FullName to add an unsaved Word document to an email but I keep getting the error "Cannot find the file..."  I tried saving the document first using ActiveDocument.SaveAs2 then attaching it.  That worked but how will I delete the file programmatically if it is open?  I also used ActiveDocument.SendMail but I cannot programmatically add Subject, Body, Recipients, etc. 

    The Word document opens with a Userform collecting data, inserting the data into the document and providing a button to create the email.

    Any ideas?

    Thanks in advance for your help.

    Wednesday, August 27, 2014 9:07 PM

Answers

  • Thanks so much Cindy!  Going through Outlook seems to work best.  Since the mail item created by .SendMail is modal, I am able to access it with GetObject.  Then manipulate the Subject, Recipients, etc.  Still trying to figure out how to get the default signature included in the the mail message and have the document attached display a different name besides "Document1.docx" or whatever number it is.

    By the way, I am already using a template.

    Thanks again for your help.


    Tuesday, September 2, 2014 7:37 PM

All replies

  • You can't attach a file that hasn't been saved - until saved, it doesn't exist as a file. So, save the file, close & attach it to the email, then delete it.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Wednesday, August 27, 2014 11:11 PM
  • If the code is running out of the Word document will closing the Word document and deleting it stop the code?  Also if a file cannot be attached until it is saved, how does ActiveDocument.SendMail attach an unsaved file to a new email document?  Is it possible to create an addin in .net that will do the same thing?  Or is it possible to reference the newly created email with ActiveDocument.SendMail and add Subject, Body and Recipients?

    Thank you so much for your help!

    Thursday, August 28, 2014 12:41 PM
  • Hi Kay

    <<The Word document opens with a Userform collecting data, inserting the data into the document and providing a button to create the email.>>

    <<If the code is running out of the Word document will closing the Word document and deleting it stop the code?>>

    Better would be to use a TEMPLATE for creating these documents, rather than re-using the same document. Since the template would be unloaded as soon as the document is closed (to say nothing of deleted) you'd also want an Add-in - whether a template add-in or COM/VSTO Add-in doesn't really matter in the context of what's possible - containing the code to perform these manipulations.

    << Is it possible to create an addin in .net that will do the same thing?>>

    .NET cannot do anything within the context of the Word application that VBA cannot do: it works with exactly the same object model limitations.

    You might have better luck pursuing the problem in the Outlook for Developers forum - someone there might have an idea how to create an attachment from a "non-file".

    You could also consider creating these documents independently of the Word application, leveraging the Word Open XML file format. Collect information via a Windows Form (or any interface you like), your code generates the Word document independently of the Word application (Word does not need to even be installed), creates the email and later removes the file. This could even be an Outlook add-in, if that would make sense in your scenario.


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, August 28, 2014 4:07 PM
    Moderator
  • Thanks so much Cindy!  Going through Outlook seems to work best.  Since the mail item created by .SendMail is modal, I am able to access it with GetObject.  Then manipulate the Subject, Recipients, etc.  Still trying to figure out how to get the default signature included in the the mail message and have the document attached display a different name besides "Document1.docx" or whatever number it is.

    By the way, I am already using a template.

    Thanks again for your help.


    Tuesday, September 2, 2014 7:37 PM