0

I am creating an Outlook Message. Sometimes the Outlook Compose window appears behind other windows.

How can I make it the top most?

String address = "someone@example.com";

Outlook.Application oApp = new Outlook.Application();
Outlook._MailItem oMailItem = (Outlook._MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
oMailItem.To = address;

oMailItem.Subject = "Help";

oMailItem.BodyFormat = Outlook.OlBodyFormat.olFormatPlain;
oMailItem.Attachments.Add("H:\\file.txt");

oMailItem.Body = "Call me";  
// body, bcc etc...
oMailItem.Display(true);

I am using WinForm and .Net 2.0 (target)

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Cocoa Dev
  • 9,361
  • 31
  • 109
  • 177
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Jul 22 '13 at 18:01
  • Use [redemption][1] to solve this problem [1]: http://stackoverflow.com/questions/17792853/how-to-make-the-outlook-compose-window-top-most – Adz Jun 18 '15 at 18:27

1 Answers1

-1

Firstly, call MailItem.GetInspector to get the Inspector object (you can then call Inspector.Display), secondly, cast Inspector to IOleWindow interface and call IOleWindows::GetWindow to retrieve the inspector's HWND. Once you have that, you can call SetForegroundWindow. One thing to keep in mind is that Windows will nto bring the window to the foreground if the parent process is not in the foreground. You would need to use AttachThreadInput function for that - see below (Delphi):

function ForceForegroundWindow(hWnd: THandle): BOOL;
var
  hCurWnd: THandle;
begin
  hCurWnd := GetForegroundWindow;
  AttachThreadInput(
    GetWindowThreadProcessId(hCurWnd, nil),
    GetCurrentThreadId, True);
  Result := SetForegroundWindow(hWnd);
  AttachThreadInput(
    GetWindowThreadProcessId(hCurWnd, nil),
    GetCurrentThreadId, False);
end;
Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78