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

◇◆丶佛笑我妖孽 提交于 2019-12-01 23:39:08

问题


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;

回答1:


Just add one more call:

MailItem.Display;
OutlookApp.ActiveWindow.Activate;

Activate brings Outlook to front.




回答2:


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

MailItem.Display(True);



回答3:


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.



来源:https://stackoverflow.com/questions/9972866/how-to-show-on-front-and-not-on-background-a-new-email-form-in-outlook-with-ol

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