3

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;
UnDiUdin
  • 14,924
  • 39
  • 151
  • 249
  • 1
    When I do this the new message window appears in front of my Delphi form. What causes this *not* to be the case for you? – Andreas Rejbrand Apr 02 '12 at 08:24
  • 1
    I've just tested it too, outlook window appears on top. May be your form has not standart settings? (`FormStyle = fsStayOnTop` for ex? - in this case outlook window appears in background, but `self.SendToBack` before `mailItem.Display` solves this problem) – teran Apr 02 '12 at 08:38
  • 2
    OP is correct. if the Outlook main window is visible the new message is opened under the Delphi form. (try to run the EXE from outside the IDE) – kobik Apr 02 '12 at 11:11
  • +1 to kobik and the OP: This problem in fact *does occur* when you do not run the program through the IDE, at least *sometimes*. There seem to be other factors involved as well. – Andreas Rejbrand Apr 02 '12 at 11:17
  • 1
    Try calling `MailItem.Display` right after `MailItem := OutlookApp.CreateItem`, *before* you assign the properties. seems to work here. also you can show it Modal `MailItem.Display(True)` if that's an option for you... – kobik Apr 02 '12 at 11:26
  • @kobik yes your solution works, if you post it as answer I will accept it. – UnDiUdin Apr 02 '12 at 12:43
  • But that makes your message window modal and from your question it feels like you want to activate your form behind; that's why I've deleted my post few hours ago. Truth is that it should resolve bringing the message window to top, but it will be modal. – TLama Apr 02 '12 at 13:08

3 Answers3

4

Just add one more call:

MailItem.Display;
OutlookApp.ActiveWindow.Activate;

Activate brings Outlook to front.

UnDiUdin
  • 14,924
  • 39
  • 151
  • 249
Andrei Galatyn
  • 3,322
  • 2
  • 24
  • 38
  • It seems a smart answer anyway i don't get the desired behavior with outlook 2010 (i didn't try with others). Outlook somehow gets focus (the new mail window flashes in orange in the application bar) but doesn't come to front. – UnDiUdin Dec 19 '13 at 08:04
  • 1
    @user: I use almost same code, but i make call .Activate when event OnOpen is triggered. And for me it works (W8, Outlook2010, XE3, UAC disabled) in both modes - modal and normal. You can take full sourcecode here and try: http://stackoverflow.com/questions/20632837/generate-new-message-in-outlook-and-display-as-modal – Andrei Galatyn Dec 19 '13 at 08:26
2

The MailItem.Display has the optional parameter Modal which should make your window modal, so try to use:

MailItem.Display(True);
TLama
  • 75,147
  • 17
  • 214
  • 392
  • 3
    But maybe the OP doesn't want the window to be modal? – Andreas Rejbrand Apr 02 '12 at 08:17
  • @Andreas, you're probably right, the note *then users can minimize it if they want* seems like a request for non modal window. – TLama Apr 02 '12 at 08:24
  • 1
    @TLama: In my environment (W8/Outlook2013/XE3) modal form is opened in background, so application form became dead-like, but modal form invisible until user activates Outlook. – Andrei Galatyn Dec 17 '13 at 14:56
1

I realize that this is an old topic, but I had the same problem recently for users with Outlook 2016. For me, the solution needed to be able to include a signature and an attachment and open the new Outlook window on top. If you call MailItem.Display(True), you lose the attachment. This was the solution that worked for me.

Notice the extra space after "Message (HTML)" in the Window name. It took me a long time to discover that the window name for new HTML emails has an extra blank space at the end.

procedure DisplayMail(Address, Subject, Body: string; Attachment: TFileName);
var
  Outlook: OleVariant;
  Mail: OleVariant;
  H : THandle;                                                              
  TempTitle : String;                                                      
begin
  TempTitle := 'Temp-'+IntToStr(Random(1000000));                         
  try
    Outlook := GetActiveOleObject('Outlook.Application');
  except
    Outlook := CreateOleObject('Outlook.Application');
  end;
  Mail := Outlook.CreateItem(olMailItem);
  Mail.To := Address;
  Mail.Subject := TempTitle;                                             
  Mail.Display(false);
  H := FindWindow('rctrl_renwnd32',PWidechar(TempTitle+' - Message (HTML) '));
  Mail.Subject := Subject;                                               
  if Body <> '' then
    Mail.HTMLBody := InsertMessage(Body,Mail.HTMLBody);
  if Attachment <> '' then                                              
    Mail.Attachments.Add(Attachment);                                  
  try
    if H <> 0 then
      SetForegroundWindow(H);
  finally
  end;
end; 

This worked for me, and it allows me to add an attachment, and it retains the default signature. I hope this helps somebody.

Kris King
  • 11
  • 1