STDMETHODIMP COutlookButton::Invoke(DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS* pDispParams, VARIANT* pVarResult, EXCEPINFO* pExcepInfo, UINT* puArgErr) { if (IID_NULL != riid) return DISP_E_UNKNOWNINTERFACE; // The only dispID supported is 1, which triggers when the "button" is clicked if (dispIdMember != 0x00000001) return DISP_E_MEMBERNOTFOUND; if (NULL == pDispParams) return E_POINTER; if (pDispParams->cArgs != 2) return DISP_E_BADPARAMCOUNT; /*CMAPIEx mapi; if(!CMAPIEx::Init() || !mapi.Login("Outlook") || !mapi.OpenMessageStore("")) { MessageBox(NULL, "Failed to initialize MAPI", "Error", MB_OK); return S_OK; } MessageBox(NULL, mapi.GetProfileName(), "Test", MB_OK);*/ // you can use this profile name as an argument for Login() when no MAPI provider is open (ie Outlook) //printf("Profile Name: %s\n",mapi.GetProfileName()); try { if( pDispParams->rgvarg[1].vt == VT_DISPATCH ) { //OutlookSecurity::SecurityManager //OutlookSecurity::SecurityManager sec; Outlook::_ExplorerPtr spExplorer = m_OLAppPtr->ActiveExplorer(); _ContactItemPtr pContactItem = NULL; CString fullName=""; if (spExplorer!=NULL) { Outlook::_NameSpacePtr nameSpace = m_OLAppPtr->GetNamespace("MAPI"); Outlook::MAPIFolderPtr currFolder = spExplorer->CurrentFolder; HRESULT hr; enum Outlook::OlItemType olt; hr = currFolder->get_DefaultItemType(&olt); if (!FAILED(hr)) { if (olt==(Outlook::OlItemType)2) { IDispatchPtr pContact = GetSelectedItem(spExplorer); if (pContact!=NULL) { pContactItem = (Outlook::_ContactItemPtr)pContact; fullName = (LPCTSTR)pContactItem->GetFullName(); } } else if (olt==(Outlook::OlItemType)0) { Outlook::MAPIFolderPtr pParentFolder = (Outlook::MAPIFolderPtr)currFolder->GetParent(); if (pParentFolder!=NULL) { IDispatchPtr pSelectedItem = GetSelectedItem(spExplorer); Outlook::_MailItemPtr pMailItem; //Outlook::_ContactItemPtr pContactItem; Outlook::MAPIFolderPtr pTemp, pTemp2; BOOL itemType = 0; // 0-other, 1-mail, 2-contact if (pSelectedItem!=NULL) { HRESULT hr = pSelectedItem->QueryInterface(&pMailItem); if (FAILED(hr)) { hr = pSelectedItem->QueryInterface(&pContactItem); if (!FAILED(hr)) itemType = 2; } else { itemType = 1; } if (itemType==1) { //mail item if ((pTemp=pParentFolder->GetParent())==NULL) { pParentFolder = currFolder; } else { while (1) { pTemp=(Outlook::MAPIFolderPtr)pParentFolder->GetParent(); if ((pTemp2=pTemp->GetParent())!=NULL) pParentFolder=pTemp; else break; } } if ((pParentFolder->GetName()==nameSpace->GetDefaultFolder(Outlook::olFolderOutbox)->GetName()) || (pParentFolder->GetName()==nameSpace->GetDefaultFolder(Outlook::olFolderSentMail)->GetName()) || (pParentFolder->GetName()==nameSpace->GetDefaultFolder(Outlook::olFolderDrafts)->GetName())) { fullName = (LPCTSTR)pMailItem->GetTo(); } else if (pParentFolder->GetName()==nameSpace->GetDefaultFolder(Outlook::olFolderInbox)->GetName()) { fullName = (LPCTSTR)pMailItem->GetSenderName(); } else { fullName = (LPCTSTR)pMailItem->GetSenderName(); /*fullName += "#####"; fullName += (LPCTSTR)pMailItem->GetTo();*/ } pContactItem = FindContact(CString(CString("[FullName] = '") + fullName + "'").AllocSysString()); } else if (itemType==2) { //contact item fullName = (LPCTSTR)pContactItem->GetFullName(); } } } } } } CString numbers; CString strTemp; if (pContactItem) { strTemp = (LPCTSTR)pContactItem->GetAssistantTelephoneNumber(); if (strTemp!="") numbers += "Assistant:" + strTemp + " | "; strTemp = (LPCTSTR)pContactItem->GetBusinessTelephoneNumber(); if (strTemp!="") numbers += "Business:" + strTemp + " | "; strTemp = (LPCTSTR)pContactItem->GetBusiness2TelephoneNumber(); if (strTemp!="") numbers += "Business2:" + strTemp + " | "; strTemp = (LPCTSTR)pContactItem->GetBusinessFaxNumber(); if (strTemp!="") numbers += "Business Fax:" + strTemp + " | "; strTemp = (LPCTSTR)pContactItem->GetCallbackTelephoneNumber(); if (strTemp!="") numbers += "Callback:" + strTemp + " | "; strTemp = (LPCTSTR)pContactItem->GetCarTelephoneNumber(); if (strTemp!="") numbers += "Car:" + strTemp + " | "; strTemp = (LPCTSTR)pContactItem->GetCompanyMainTelephoneNumber(); if (strTemp!="") numbers += "Company Main:" + strTemp + " | "; strTemp = (LPCTSTR)pContactItem->GetHomeTelephoneNumber(); if (strTemp!="") numbers += "Home:" + strTemp + " | "; strTemp = (LPCTSTR)pContactItem->GetHome2TelephoneNumber(); if (strTemp!="") numbers += "Home2:" + strTemp + " | "; strTemp = (LPCTSTR)pContactItem->GetHomeFaxNumber(); if (strTemp!="") numbers += "Home Fax:" + strTemp + " | "; strTemp = (LPCTSTR)pContactItem->GetISDNNumber(); if (strTemp!="") numbers += "ISDN:" + strTemp + " | "; strTemp = (LPCTSTR)pContactItem->GetMobileTelephoneNumber(); if (strTemp!="") numbers += "Mobile:" + strTemp + " | "; strTemp = (LPCTSTR)pContactItem->GetOtherTelephoneNumber(); if (strTemp!="") numbers += "Other:" + strTemp + " | "; strTemp = (LPCTSTR)pContactItem->GetOtherFaxNumber(); if (strTemp!="") numbers += "Other Fax:" + strTemp + " | "; strTemp = (LPCTSTR)pContactItem->GetPagerNumber(); if (strTemp!="") numbers += "Pager:" + strTemp + " | "; strTemp = (LPCTSTR)pContactItem->GetPrimaryTelephoneNumber(); if (strTemp!="") numbers += "Primary:" + strTemp + " | "; strTemp = (LPCTSTR)pContactItem->GetRadioTelephoneNumber(); if (strTemp!="") numbers += "Radio:" + strTemp + " | "; strTemp = (LPCTSTR)pContactItem->GetTelexNumber(); if (strTemp!="") numbers += "Telex:" + strTemp + " | "; strTemp = (LPCTSTR)pContactItem->GetTTYTDDTelephoneNumber(); if (strTemp!="") numbers += "TTY/TDD:" + strTemp + " | "; } // We can now get at the "button" which triggered this event. LPDISPATCH pButton = pDispParams->rgvarg[1].pdispVal; CString strWindowTitle = "BSOC Main Application Window"; CString strDataToSend; if (fullName=="") { strDataToSend = _T("Dial"); } else { CString ContactName; if (pContactItem) { ContactName = CString((LPCTSTR)pContactItem->GetFirstName()) + " | " + CString((LPCTSTR)pContactItem->GetMiddleName()) + " | " + CString((LPCTSTR)pContactItem->GetLastName()); } else { ContactName = fullName; } strDataToSend = _T("Dial#####" + ContactName + "#####" + numbers); } LRESULT copyDataResult; CWnd *pOtherWnd = CWnd::FindWindow(NULL, strWindowTitle); if (pOtherWnd) { COPYDATASTRUCT cpd; cpd.dwData = 0; cpd.cbData = strDataToSend.GetLength(); cpd.lpData = (void*)strDataToSend.GetBuffer(cpd.cbData); copyDataResult = pOtherWnd->SendMessage(WM_COPYDATA, (WPARAM)NULL, (LPARAM)&cpd); // copyDataResult has value returned by other app } else { AfxMessageBox(CString(APP_NAME) + " is not running."); } } } catch (...) { } /*mapi.Logout(); CMAPIEx::Term();*/ return S_OK; }
IDispatchPtr CreateNewEmail(IDispatchPtr spDispOutlook, IDispatchPtr spDispNameSpace) { if(spDispOutlook == 0) throw Workshare::ArgumentNullException(_T("spDispOutlook"), _T("Outlook Application pointer is null.")); if(spDispNameSpace == 0) throw Workshare::ArgumentNullException(_T("spDispNameSpace"), _T("MAPI NameSpace pointer is null.")); Outlook::_ApplicationPtr spOutlook = spDispOutlook; Outlook::_NameSpacePtr spNameSpace = spDispNameSpace; Outlook::MAPIFolderPtr spDraftsFolder = spNameSpace->GetDefaultFolder(Outlook::olFolderDrafts); Outlook::_ItemsPtr spDraftItems = spDraftsFolder->Items; return spDraftItems->Add(); }
IDispatchPtr FindLinkedEmail(IDispatchPtr spDispOutlook, IDispatchPtr spDispNameSpace, Workshare::Mail::Mapi::MapiSession& session, const std::tstring& sMessageId) { if(spDispOutlook == 0) throw Workshare::ArgumentNullException(_T("spDispOutlook"), _T("Outlook Application pointer is null.")); if(spDispNameSpace == 0) throw Workshare::ArgumentNullException(_T("spDispNameSpace"), _T("MAPI NameSpace pointer is null.")); if(sMessageId.empty()) throw Workshare::ArgumentException(_T("sMessageId"), _T("The message ID of the email to find was not specified.")); size_t pos = sMessageId.find(_T(';')); if(-1 == pos) throw Workshare::ArgumentException(_T("sMessageId"), _T("Invalid message ID (should be in format EntryId;SearchKey[;InternetId])")); std::tstring sEntryId = sMessageId.substr(0, pos); std::tstring sSearchKeyAndInternetId = sMessageId.substr(pos + 1); std::tstring sSearchKey; std::tstring sInternetId; pos = sSearchKeyAndInternetId.find(_T(';')); if(-1 != pos) { sSearchKey = sSearchKeyAndInternetId.substr(0, pos); sInternetId = sSearchKeyAndInternetId.substr(pos + 1); } else sSearchKey = sSearchKeyAndInternetId; Outlook::_ApplicationPtr spOutlook = spDispOutlook; Outlook::_NameSpacePtr spNameSpace = spDispNameSpace; IDispatchPtr spLinkedEmail; HRESULT hr = spNameSpace->raw_GetItemFromID(_bstr_t(sEntryId.c_str()), vtMissing, &spLinkedEmail); if(FAILED(hr)) { std::tstring sStoreId; Workshare::Mail::Mapi::FindItemBySearchKey(session, sSearchKey, sEntryId, sStoreId); _variant_t vtStoreId(sStoreId.c_str()); hr = spNameSpace->raw_GetItemFromID(_bstr_t(sEntryId.c_str()), vtStoreId, &spLinkedEmail); if(FAILED(hr)) throw Workshare::Com::ComException(_T("Outlook failed to open email"), hr, spNameSpace); //If this fails we could create a function FindItemByInternetId(session, sInternetId, sEntryId, sStoreId); } return spLinkedEmail; }
IDispatchPtr OpenEmail(IDispatchPtr spDispOutlook, IDispatchPtr spDispNameSpace, const std::tstring& entryId) { if(spDispOutlook == 0) throw Workshare::ArgumentNullException(_T("spDispOutlook"), _T("Outlook Application pointer is null.")); if(spDispNameSpace == 0) throw Workshare::ArgumentNullException(_T("spDispNameSpace"), _T("MAPI NameSpace pointer is null.")); Outlook::_ApplicationPtr spOutlook = spDispOutlook; Outlook::_NameSpacePtr spNameSpace = spDispNameSpace; IDispatchPtr spMailItem; HRESULT hr = spNameSpace->raw_GetItemFromID(_bstr_t(entryId.c_str()), vtMissing, &spMailItem); if(FAILED(hr)) throw Workshare::Com::ComException(_T("Outlook failed to open the email message"), hr, spNameSpace); return spMailItem; }
Outlook::_MailItemPtr FindEmail(IDispatchPtr spDispOutlook, IDispatchPtr spDispNameSpace, const std::tstring& subject) { if(spDispOutlook == 0) throw Workshare::ArgumentNullException(_T("spDispOutlook"), _T("Outlook Application pointer is null.")); if(spDispNameSpace == 0) throw Workshare::ArgumentNullException(_T("spDispNameSpace"), _T("MAPI NameSpace pointer is null.")); Outlook::_ApplicationPtr spOutlook = spDispOutlook; Outlook::_NameSpacePtr spNameSpace = spDispNameSpace; Outlook::MAPIFolderPtr spSentItems; HRESULT hr = spNameSpace->raw_GetDefaultFolder(Outlook::olFolderSentMail, &spSentItems); if(FAILED(hr)) throw Workshare::Com::ComException(_T("Failed to get Sent Items folder"), hr); if(spSentItems == 0) throw Workshare::NullReferenceException(_T("Failed to get Sent Items folder")); Outlook::_MailItemPtr spEmailItem; spEmailItem = FindEmailInFolder(spSentItems, subject); if(0 != spEmailItem) return spEmailItem; //Email might still be in the Outbox so look there Outlook::MAPIFolderPtr spOutbox; hr = spNameSpace->raw_GetDefaultFolder(Outlook::olFolderOutbox, &spOutbox); if(FAILED(hr)) throw Workshare::Com::ComException(_T("Failed to get Outbox folder"), hr); if(spOutbox == 0) throw Workshare::NullReferenceException(_T("Failed to get Outbox folder")); spEmailItem = FindEmailInFolder(spOutbox, subject); if(0 != spEmailItem) return spEmailItem; //Email might have been sent while we were lounging around in the Outbox so go back to Sent Items spEmailItem = FindEmailInFolder(spSentItems, subject); if(0 != spEmailItem) return spEmailItem; std::tostringstream errMsg; errMsg << _T("Failed to find email in Sent Items with Subject = \"") << subject << _T("\"") << std::ends; throw Workshare::Com::ComException(errMsg.str().c_str(), MAPI_E_NOT_FOUND); }