Jump to content
bazzer747

Outlook OLE Attachments

Recommended Posts

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,

 

    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

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

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

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×