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;