Outlook - 从显示名称中获取电子邮件地址。适用于Python,而不是C语言

时间:2015-10-19 13:46:37

标签: c++ c outlook com ole

我正在尝试将一些代码从Python转换为C和OLE。代码从Outlook显示名称中获取电子邮件地址。

Python代码的工作原理如下:

    recipient = self._outlook.Session.CreateRecipient(name)

    recipient.Resolve()

    if self._first:
        print(inspect.getmembers(recipient))
        self._first = False
        print(recipient.Resolved)
        print(recipient.AddressEntry)
        if 'EX' == recipient.AddressEntry.Type:
            exchange_user = recipient.AddressEntry.GetExchangeUser()
            email = exchange_user.PrimarySmtpAddress
        elif 'SMTP' == recipient.AddressEntry.Type:
            email = recipient.AddressEntry.Address
        print(email)

反过来,它基本上是VBA code on MSDN的蟒蛇化版本。

然而,在C和OLE中我只是不明白我只能在MSDN和其他论坛上找到带有MFC示例(我不感兴趣)的C#,VBA和C ++,以及使用专有的建议框架。

到目前为止,我在C中拥有的是什么。已使用outlookapp成功初始化CoCreateInstance()

IDispatch *pNS = NULL;
{
    VARIANT x;
    x.vt = VT_BSTR;
    x.bstrVal = SysAllocString(L"MAPI");

    VARIANT result;
    VariantInit(&result);
    AutoWrap(DISPATCH_METHOD, &result, outlookapp, L"GetNamespace", 1, x);
    pNS = result.pdispVal;

    SysFreeString(x.bstrVal);
    VariantClear(&x);
}


// Log on by using a dialog box to choose the profile.
{
    VARIANT vtShowDialog;
    vtShowDialog.vt = VT_BOOL;
    vtShowDialog.boolVal = VARIANT_TRUE;
    VARIANT vtNewSession;
    vtNewSession.vt = VT_BOOL;
    vtNewSession.boolVal = VARIANT_TRUE;

    if (FAILED(AutoWrap(DISPATCH_METHOD, NULL, pNS, L"Logon", 4, vtNewSession,
        vtShowDialog, vtMissing, vtMissing))) {
        printf("Logon failed.\n"); Sleep(15000); ExitProcess(1);
    }
}

printf("Done logging in to Outlook\n");

IDispatch *recipient = NULL;
{
    VARIANT x;
    x.vt = VT_BSTR;
    x.bstrVal = SysAllocString(L"John Doe");

    VARIANT result;
    VariantInit(&result);
    HRESULT hr = AutoWrap(DISPATCH_METHOD, &result, pNS, L"CreateRecipient", 1, &x);
    if (FAILED(hr)) {
        printf("Can not CreateRecipient(), error: %x\n", hr); Sleep(60000); ExitProcess(1);
    }
    recipient = result.pdispVal;
}

{
    VARIANT result;
    VariantInit(&result);
    HRESULT hr = AutoWrap(DISPATCH_METHOD, &result, recipient, L"Resolve", 0);
    if (FAILED(hr)) {
        printf("Can not call Resolve()\n");
        Sleep(3000); ExitProcess(1);
    }
}

代码在“Can not CreateRecipient()”printf消息上消失,错误0x800706F4对应于“空引用指针被传递给存根。”

AutoWrap还会编写自己的消息:IDispatch::Invoke("CreateRecipient"=0000210a) failed w/err 0x800706f4

它来自AutoWrap中的代码:

// Make the call 
hr = pDisp->Invoke(
    dispID,
    IID_NULL,
    LOCALE_SYSTEM_DEFAULT,
    autoType,
    &dp,
    pvResult,
    NULL,
    NULL
); 

if (FAILED(hr))  
{ 
    wprintf(L"IDispatch::Invoke(\"%s\"=%08lx) failed w/err 0x%08lx\n",  
        ptName, dispID, hr); 
    return hr; 
} 

最终目标是将Outlook中的地址数据提供给Excel,并且我已经使用OLE自动化工具。真的很高兴任何想法 - 我真的试图找到有关这方面的信息,但没有发现Outlook如何在这个级别实际工作。

1 个答案:

答案 0 :(得分:0)

正如汉斯帕森特所看到的,我只是在这一行输入了&x而不是x

HRESULT hr = AutoWrap(DISPATCH_METHOD, &result, pNS, L"CreateRecipient", 1, &x);

::

HRESULT hr = AutoWrap(DISPATCH_METHOD, &result, pNS, L"CreateRecipient", 1, x);