title: Get specific Outlook attachment (OLE Automation C++)
body: <p>I can't get a specific Outlook (2013) email attachment. I'm working on a little project to learn about MS Office Automation using C++ and I'm testing Outlook automation. In partcular I would like to download a specific email attachment but I can't access to that item. For example, if I have in my inbox an email with 4 attachments I want get the 2nd one.
  I tried with this code but the HRESULT returned value from the AutoWrap() method is always not valid:</p>
  
  <pre><code>VARIANT result;
  VariantInit(&amp;result);
  
  CComPtr&lt;IDispatch&gt; pAttachments; // email attachments
  HRESULT hRes = AutoWrap(DISPATCH_PROPERTYGET, &amp;result, pOfficeItem, L"Attachments", 0);
  if (!result.pdispVal || FAILED(hRes)) return EditorError; // EditorError is an Enum
  
  pAttachments = result.pdispVal;
  
  VariantInit(&amp;result);
  hRes = AutoWrap(DISPATCH_PROPERTYGET, &amp;result, pAttachments, L"Count", 0);
  if (FAILED(hRes)) return EditorError;
  
  int aNumber = result.iVal; // it works, if i have an email with 4 attachments then aNumber is 4
  if(aNumber &gt; 0){
    VARIANT attachmentIndex;
    attachmentIndex.vt = VT_I4;
    attachmentIndex.llVal = 0; // I want the 1st attachment
  
    VariantInit(&amp;result);
    CComPtr&lt;IDispatch&gt; pAttachmentItem;
  
   hRes = AutoWrap(DISPATCH_PROPERTYGET, &amp;result, pAttachments, L"Item", 1, attachmentIndex);
    if (FAILED(hRes)) return EditorError; // here it returns EditorError 
  }
  ... DO SOMETHING ...
  </code></pre>
  
  <p>where AutoWrap() is the method recommended by MS in order to interact with the MS Application (<a href="http://www.codeproject.com/Articles/34998/MS-Office-OLE-Automation-Using-C" rel="nofollow">http://www.codeproject.com/Articles/34998/MS-Office-OLE-Automation-Using-C</a>).</p>