0

I have the following function that I use to send mail using MS Outlook 2010 From time to time Outlook doesn't appear in front of my app (a MDI app pmNone, poDefaultPosOnly) I haven't been able to find any continuity in when Outlook pops up in front and when it just opens in background (icon is flashing on statusbar) My question is then: are there any method of forcing Outlook to popup in front of all other apps?

function SendMailOutlook(const aFrom, aSubject, aBody, aTo, aCc, aBcc: string; aMailFiles: TStringList; aReceipt: boolean; aPreview: boolean = True): boolean;
var
  Outlook: OleVariant;
  MailItem: OleVariant;
  i: integer;
const
  olMailItem = $00000000;
begin
  try
    try
      Outlook := GetActiveOleObject('Outlook.Application');
    except
      Outlook := CreateOleObject('Outlook.Application');
    end;
    MailItem := Outlook.CreateItem(olMailItem);
    if olAccountValid(aFrom) then
      MailItem.SendUsingAccount := Outlook.Session.Accounts.Item(aFrom);
    MailItem.To := aTo;
    MailItem.Cc := aCc;
    MailItem.Bcc := aBcc;
    MailItem.Subject := aSubject;
    MailItem.Body := aBody;
    for i := 0 to aMailFiles.Count - 1 do
      MailItem.Attachments.Add(aMailFiles.Strings[i]);
    MailItem.ReadReceiptRequested := aReceipt;
    MailItem.OriginatorDeliveryReportRequested := aReceipt;
    if aPreview = True then
      MailItem.Display(True)
    else
      MailItem.Send;
    Result := MailItem.Sent;
  except
    on E:Exception do
      begin
        Logfile.Error('U_Mailing.Outlook.SendMailOutlook: ' + E.Message);
        Result := False;
      end;
  end;
end;

I am no sure how to show the code I got working so I will just edit my question and add it there. Please excuse if that isn't the way to do it.

function SendMailOutlook(const aFrom, aSubject, aBody, aTo, aCc, aBcc: string; aMailFiles: TStringList; aReceipt: boolean; aPreview: boolean = True): boolean;
var
  Outlook: OleVariant;
  MailItem: OleVariant;
  i: integer;
  MailInspector: variant;
const
  olMailItem = $00000000;
begin
  try
    try
      Outlook := GetActiveOleObject('Outlook.Application');
    except
      Outlook := CreateOleObject('Outlook.Application');
    end;
    MailItem := Outlook.CreateItem(olMailItem);
    if olAccountValid(aFrom) then
      MailItem.SendUsingAccount := Outlook.Session.Accounts.Item(aFrom);
    MailItem.To := aTo;
    MailItem.Cc := aCc;
    MailItem.Bcc := aBcc;
    MailItem.Subject := aSubject;
    MailItem.Body := aBody;
    for i := 0 to aMailFiles.Count - 1 do
      MailItem.Attachments.Add(aMailFiles.Strings[i]);
    MailItem.ReadReceiptRequested := aReceipt;
    MailItem.OriginatorDeliveryReportRequested := aReceipt;

    if aPreview = True then
      begin
        MailInspector := MailItem.GetInspector;
        MailInspector.Display(True);
      end
    else
      MailItem.Send;
    Result := MailItem.Sent;
  except
    on E:Exception do
      begin
        Logfile.Error('U_Mailing.Outlook.SendMailOutlook: ' + E.Message);
        Result := False;
      end;
  end;
end;
OZ8HP
  • 1,443
  • 4
  • 31
  • 61
  • See [How to show on front (and not on background) a new email form in Outlook with OLE?](http://stackoverflow.com/q/9972866/576719). – LU RD May 04 '15 at 08:31
  • Have tried Outlook.ActiveWindow.Activate; but that makes the program crash. – OZ8HP May 04 '15 at 08:56
  • Did you also remove the modal parameter from `MailItem.Display`? – LU RD May 04 '15 at 11:05
  • I did now :-) It looks like this does the trick but I will check some more – OZ8HP May 05 '15 at 07:32
  • I have been checking it and it doesn't work with my Outlook 2010 and it doesn't matter if Outlook is running or not when the mail is created. – OZ8HP May 05 '15 at 18:25

2 Answers2

1

I bielive you can use the SetForegroundWindow function which brings the thread that created the specified window into the foreground and activates the window. Keyboard input is directed to the window, and various visual cues are changed for the user. The system assigns a slightly higher priority to the thread that created the foreground window than it does to other threads.

You can get the Outlook window handle casting the Explorer or Inspector objects to the IOleWindow interface.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
1

You will need to use IOleWindow / AttachThreadInput / SetForegroundWindow to do that.

See how to make the Outlook Compose window top most?.

Also see c# bring outlook window to front

Community
  • 1
  • 1
Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78