Search code examples
delphioutlookoleoutlook-2010

How to show on front (and not on background) a new email form in Outlook with OLE?


I am using OLE with Delphi to communicate from my delphi app to Outlook.

I am opening the new email form in Outlook using the following code. The problem is that the form is on background, so if the form from which I am generating the email form is maximized it will "cover" the Outlook new mail form.

How can I make that form appear it on top? (not "sticking on top", but simply that it appears on top, then a user can mimimize it if they want).

This is the code:

try
    OutlookApp := GetActiveOleObject('Outlook.Application');
  except
    OutlookApp := CreateOleObject('Outlook.Application');
  end;
  try
    MailItem := OutlookApp.CreateItem(olMailItem);
    MailItem.To := 'Test@mail.com';     
    MailItem.Subject := 'This is the subject';
    MailItem.HTMLBody    := '<HTML>Test</HTML>';
    MailItem.Display;
  finally
    OutlookApp    := VarNull;
  end;
end;

Solution

  • Just add one more call:

    MailItem.Display;
    OutlookApp.ActiveWindow.Activate;
    

    Activate brings Outlook to front.