I am writing a C # application that uses Office Outlook Interop (2010; version 14) to access email data through MAPI.
So far, I have had to manually and repeatedly click “Allow” in Outlook “ The program is trying to access information about email addresses stored in Outlook ” (screenshot in http://i.stack.imgur.com/gj8to.png ) .
I tried to write a method to automatically push, but to no avail. A click occurs, but Outlook Interop throws the following exception during the next read operation:
HRESULT exception: 0x80004004 (E_ABORT)
Here is an excerpt from the code that SendMessage uses to click the Allow button:
private const Int32 BM_CLICK = 0x00F5;
private const Int32 GWL_STYLE = -16;
private const Int32 CB_SETCURSEL = 0x14E;
private const Int32 WM_LBUTTONDOWN = 0x201;
private const Int32 WM_LBUTTONUP = 0x202;
private const string dialogClass = "#32770";
private const string dialogTitle = "Microsoft Outlook";
const string tickBoxTitle = @"&Allow access for";
const string tickBoxClass = @"Button";
private const int ThreadSleepTime = 1000;
private const int ThreadUiSleepTime = 20;
IntPtr popupHandle;
IntPtr tickHandle = IntPtr.Zero;
IntPtr comboHandle = IntPtr.Zero;
IntPtr allowHandle = IntPtr.Zero;
while ((popupHandle = FindWindow(dialogClass, dialogTitle)) != IntPtr.Zero)
{
tickHandle = FindWindowEx(popupHandle, tickHandle, tickBoxClass, tickBoxTitle);
comboHandle = FindWindowEx(popupHandle, tickHandle, null, null);
allowHandle = FindWindowEx(popupHandle, comboHandle, null, null);
while ((GetWindowLong(comboHandle, GWL_STYLE) & 0x8000000) != 0)
{
SendMessage(tickHandle, BM_CLICK, new IntPtr(1), IntPtr.Zero);
System.Threading.Thread.Sleep(ThreadUiSleepTime);
}
SendMessage(comboHandle, CB_SETCURSEL, 3, 0);
System.Threading.Thread.Sleep(ThreadUiSleepTime);
SendMessage(allowHandle, WM_LBUTTONDOWN, IntPtr.Zero, new IntPtr(MakeLParam(5, 5)));
System.Threading.Thread.Sleep(ThreadUiSleepTime);
SendMessage(allowHandle, WM_LBUTTONUP, IntPtr.Zero, new IntPtr(MakeLParam(5, 5)));
}
Why is SendMessage not working and how can I do this?