Reputation: 1513
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;
Upvotes: 0
Views: 1440
Reputation: 66316
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
Upvotes: 1
Reputation: 49455
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.
Upvotes: 1