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

余生长醉 提交于 2019-12-01 21:07:17
Andrei Galatyn

Just add one more call:

MailItem.Display;
OutlookApp.ActiveWindow.Activate;

Activate brings Outlook to front.

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

MailItem.Display(True);

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!