【发布时间】:2011-12-23 00:09:35
【问题描述】:
我正在用 C# 编写一个应用程序,它使用 Office Outlook Interop(2010;版本 14)通过 MAPI 访问电子邮件数据。
到目前为止,我必须在 Outlook 的“程序正在尝试访问存储在 Outlook 中的电子邮件地址信息”安全弹出窗口中手动反复单击“允许”(屏幕截图位于 http://i.stack.imgur.com/gj8to.png)。
我尝试编写一个自动点击的方法,但没有成功。单击发生,但 Outlook Interop 在下一次读取操作期间引发以下异常:
来自 HRESULT 的异常:0x80004004 (E_ABORT)
以下是使用 SendMessage 单击“允许”按钮的代码摘录:
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)
{
// Get object handles
tickHandle = FindWindowEx(popupHandle, tickHandle, tickBoxClass, tickBoxTitle);
comboHandle = FindWindowEx(popupHandle, tickHandle, null, null);
allowHandle = FindWindowEx(popupHandle, comboHandle, null, null);
// Click on tickbox until the combobox is enabled
while ((GetWindowLong(comboHandle, GWL_STYLE) & 0x8000000) != 0)
{
SendMessage(tickHandle, BM_CLICK, new IntPtr(1), IntPtr.Zero);
System.Threading.Thread.Sleep(ThreadUiSleepTime);
}
// Set dropdown box selection index to 3rd row
SendMessage(comboHandle, CB_SETCURSEL, 3, 0);
System.Threading.Thread.Sleep(ThreadUiSleepTime);
// Click Allow button
//SendMessage(allowHandle, BM_CLICK, new IntPtr(1), IntPtr.Zero);
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)));
}
为什么 SendMessage 不起作用,我怎样才能使它起作用?
【问题讨论】:
标签: c# winapi outlook office-interop