I am working on a project, in this project I am add a feature, if I click on a button then it will open the outlook and send a file to another user. in my system the build is working fine, version of the outlook is 2010.
In another system the feature is not working. if he clicked on the button then an error message is coming
it saying: NO Such interface supported. in his system version of the outlook is 2021.
//This is the code what i have done
void CWeavesView::OnSendButton()
{
HRESULT hr = CoInitialize(NULL);
if (FAILED(hr))
{
AfxMessageBox(_T("Failed to initialize COM library."));
return;
}
// Create an Outlook application instance
CComPtr<IDispatch> spOutlookApp;
hr = spOutlookApp.CoCreateInstance(L"Outlook.Application");
if (FAILED(hr))
{
CString errorMsg;
_com_error err(hr);
errorMsg.Format(_T("Failed to create Outlook application instance. Error: %s"), err.ErrorMessage());
AfxMessageBox(errorMsg);
CoUninitialize();
return;
}
// Get the MAPI namespace
CComPtr<IDispatch> spNamespace;
VARIANT vtEmpty;
VariantInit(&vtEmpty);
hr = InvokeMethod(spOutlookApp, L"GetNamespace", &vtEmpty, 1,
CComVariant(L"MAPI"));
if (FAILED(hr))
{
CString errorMsg;
_com_error err(hr);
errorMsg.Format(_T("Failed to get MAPI namespace. Error: %s"), err.ErrorMessage());
AfxMessageBox(errorMsg);
CoUninitialize();
return;
}
spNamespace = vtEmpty.pdispVal;
// Create a new mail item
CComPtr<IDispatch> spMailItem;
hr = InvokeMethod(spOutlookApp, L"CreateItem", &vtEmpty, 1,
CComVariant(0)); // 0 = olMailItem
if (FAILED(hr))
{
CString errorMsg;
_com_error err(hr);
errorMsg.Format(_T("Failed to create mail item. Error: %s"), err.ErrorMessage());
AfxMessageBox(errorMsg);
CoUninitialize();
return;
}
spMailItem = vtEmpty.pdispVal;
// Set the subject
CComVariant vtSubject(L"Subject of the email");
hr = PutProperty(spMailItem, L"Subject", &vtSubject);
if (FAILED(hr))
{
CString errorMsg;
_com_error err(hr);
errorMsg.Format(_T("Failed to set subject. Error: %s"), err.ErrorMessage());
AfxMessageBox(errorMsg);
}
// Set the body
CComVariant vtBody(L"Body of the email");
hr = PutProperty(spMailItem, L"Body", &vtBody);
if (FAILED(hr))
{
CString errorMsg;
_com_error err(hr);
errorMsg.Format(_T("Failed to set body. Error: %s"), err.ErrorMessage());
AfxMessageBox(errorMsg);
}
// Attach the file
CComPtr<IDispatch> spAttachments;
hr = GetProperty(spMailItem, L"Attachments", &spAttachments);
if (FAILED(hr))
{
CString errorMsg;
_com_error err(hr);
errorMsg.Format(_T("Failed to get attachments collection. Error: %s"), err.ErrorMessage());
AfxMessageBox(errorMsg);
CoUninitialize();
return;
}
CString file_path_Name = _T("F:\1.TIS");
CComVariant varFilePath(file_path_Name);
hr = InvokeMethod(spAttachments, L"Add", &vtEmpty, 1, varFilePath);
if (FAILED(hr))
{
CString errorMsg;
_com_error err(hr);
errorMsg.Format(_T("Failed to attach file. Error: %s"), err.ErrorMessage());
AfxMessageBox(errorMsg);
}
// Display the email
hr = InvokeMethod(spMailItem, L"Display", nullptr, 0);
if (FAILED(hr))
{
CString errorMsg;
_com_error err(hr);
errorMsg.Format(_T("Failed to display email. Error: %s"), err.ErrorMessage());
AfxMessageBox(errorMsg);
}
// Cleanup
CoUninitialize();
}
11