Archive for the ‘ole’ tag
Getting the handle off any Outlook window
When embedding some window inside Microsoft Outlook, it is not understandable that at some point you need the handle of some an Outlook window object, an Outlook Inspector or an Explorer. The Outlook Object Model does not expose a method to obtain the handle to a window. This is based on some information from Dmitry Streblechenko.
Yes, but not in VBA: you need to QI the Inspector (or Explorer) object for the IOleWindow interface, then call IOleWindow::GetWindow()
If you work with low-level Microsoft Outlook you will eventually find some information, very often forum posts, answered by Dmitry. You will also quickly learn that he is very often right.
I have written the following code in the very last days I have worked on the EchoTracker. It was part of refactorization I have not had time to finish so this code is UNTESTED. This is based solely on my interpretation of the indication of Dmitry.
/// <summary>
/// Embed the Outlook panel in *any* Outlook explorer. Thanks Dimitry.
/// http://www.pcreview.co.uk/forums/thread-1837879.php
/// </summary>
public static MSOWindow GetOutlookWindow(Outlook.Explorer olExp)
{
IntPtr olExpUnk = IntPtr.Zero;
IntPtr oleWinPtr = IntPtr.Zero;
IntPtr hWnd = IntPtr.Zero;
Guid oleWinGuid = typeof(IOleWindow).GUID;
IOleWindow oleWin = null;
try
{
olExpUnk = Marshal.GetIUnknownForObject(olExp);
oleWinPtr = IntPtr.Zero;
if (Marshal.QueryInterface(olExpUnk, ref oleWinGuid, out oleWinPtr) != 0)
throw new Exception("QueryInterface failed.");
oleWin = (IOleWindow)Marshal.GetObjectForIUnknown(oleWinPtr);
if (oleWin == null)
throw new Exception("GetObjectForIUnknown failed.");
oleWin.GetWindow(out oleWinPtr);
}
finally
{
if (oleWin != null) Marshal.ReleaseComObject(oleWin);
}
return new MSOWindow(hWnd);
}
For this code to hopefully work, you need to have the COM interop declaration for the IOleWindow interface. You can find this information on pinvoke.net.
Please, if you stumble upon that code, and happen to have a need for it, use it, or adapt it to your need, leave a comment on this post. I repeat that this is untested. I have no plan to test it, I don’t have a machine on which I can develop Microsoft Outlook.
