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

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

I am opening a new email form in Outlook using the following code. The problem is that the form is in the background, so if the form from which I am generating the email form is maxed out, it "covers" the new Outlook mail form.

How can I make this form on top? (not "stick on top", just that it is displayed on top, then the user can imitate it if he wants).

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;

      

+3


source to share


3 answers


Just add another call:

MailItem.Display;
OutlookApp.ActiveWindow.Activate;

      



Activate Bring Outlook to Front.

+4


source


MailItem.Display

has an optional parameter Modal

that should make your window modal, so try using:



MailItem.Display(True);

      

+2


source


I realize this is an old topic, but I had the same problem lately for users with Outlook 2016. For me, the solution was to be able to include a signature and attachment and open a 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 find out that the window name for new HTML emails has an extra white 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 keeps the signature by default. Hope this helps someone.

+1


source







All Articles