bazzer747 25 Posted July 6, 2020 Hi, I mainly use Outlook to send emails, and on some I attach a document. My code mainly revolves around this line: vMailItem.Attachments.Add( filename ); (vMailItem being the Outlook item object. However, what I'm finding is that after I've sent an email with a file, if I want to send another email but without a file, the file is still attached to the email. I've cleared the edit box which holds the file name (which I use to get the name of the file). I use a boolean variable to say whether there is an attachment or not and that code does bypass the above line, but the previous file is still being attached. Is there a method which can remove any previous file attachments to an email, like 'vMailItem.Attachments.Remove' ? Share this post Link to post
Anders Melander 1783 Posted July 6, 2020 Please show us your code (minimal example). I'm mainly interested in how/when you create vMailItem. Share this post Link to post
bazzer747 25 Posted July 6, 2020 Anders, try Outlook:= GetActiveOleObject('Outlook.Application'); except Outlook:= CreateOleObject('Outlook.Application'); end; vMailItem := Outlook.CreateItem( olMailItem ); .. and later on ... if gvbAttached = True then vMailItem.Attachments.Add( gvAttachedFile ); //(gvbAttached being boolean which determines if file is to be attached, and gvbAttached being the location/name of that file). Share this post Link to post
Anders Melander 1783 Posted July 6, 2020 Okay but do you create a new mail item for each mail or do you reuse the first one you create? Share this post Link to post
bazzer747 25 Posted July 6, 2020 After I've done a vMailItem.Send (or .Display), I issue a VarClear( Outlook ). This is for every individual email sent. Share this post Link to post
Anders Melander 1783 Posted July 6, 2020 My guess is that you're keeping a reference to one of the outlook items but that doesn't explain why you get the same attachments when you create a new message. Try starting without Outlook running. When you do GetActiveOleObject('Outlook.Application') Outlook should start and when you do VarClear( Outlook ) it should terminate again. Use taskmanager or something like it to verify that the Outlook process has terminated. if everything else fails you could also just empty the Attachments collection after you have sent the message. Ie while (vMailItem.Attachments.Count > 0) do vMailItem.Attachments.Remove(0) Share this post Link to post
bazzer747 25 Posted July 6, 2020 Anders, That code works perfectly, many thanks, so my problem is solved. With Outlook closed, when I run the code the email is created and I can see Microsoft Outlook (32 bit) in Task Manager. Even after the VarClear( Outlook ) executes Outlook is still there. When I close or send the email Outlook then closes. When the line GetActiveOleObject('Outlook.Application') is run I get an error message: 'Operation unavailable', but the email still gets created and displayed/sent. This only occurs in debug mode. When I run in normal mode I don't get an error message, but as above Outlook opens only on the vMailItem.Display line, and only closes when I send/delete the actual email. Share this post Link to post