title: Deleting inline attachments
body: <p>I'm trying to search selected emails and delete the attachments. I did a bit of research and ended up going with the Word.Document route. </p>
  
  <p>I had a previous tidbit of code that deleted all the attachments but was leaving behind a dotted line box that said the image wasn't available. </p>
  
  <p>I'm trying to mesh the two as this one below does not delete attachments but only the inline shapes.</p>
  
  <p>Code that deletes inline images:</p>
  
  <pre><code>Sub DeleteAllAttachmentsFromSelectedMessages()
  Dim selectedItems As Selection
  Dim messageObject As Object
  Dim documentsObject As Object
  Dim shp As InlineShape
  Dim doc As Object
  Dim shpRange As Object
  Const wdInlineShapePicture As Long = 3
  Const wdInlineShapesEmbeddedOLEObject As Long = 1
  
  ' Set reference to the Selection.
  Set selectedItems = ActiveExplorer.Selection
  
  For Each messageObject In selectedItems
      Set doc = messageObject.GetInspector.WordEditor
      ' doc.UnProtect
      For Each shp In doc.InlineShapes
          Select Case shp.Type
              Case wdInlineShapePicture, wdInlineShapesEmbeddedOLEObject
                  Set shpRange = doc.Range(shp.Range.Characters.First.Start, shp.Range.Characters.Last.End)
                  shpeRange.Text = "Attachment Removed" ' Replace shape with text
              Case Else
                  ' Other shapes not supported yet
          End Select
      ' doc.Protect
      messageObject.Save
      Next
  Next
  
  MsgBox "Attachments were removed.", vbOKOnly, "Message"
  
  Set selectedItems = Nothing
  Set messageObject = Nothing
  Set documentsObject = Nothing
  Set shp = Nothing
  Set doc = Nothing
  Set shpRange = Nothing
  End Sub
  </code></pre>
  
  <p>For the code I was using to delete all attachments:</p>
  
  <pre><code>Sub DeleteAllAttachmentsFromSelectedMessages()
  Dim attachmentsObject As Attachments
  Dim selectedItems As Selection
  Dim messageObject As Object
  Dim attachmentCount As Long
  
  Set selectedItems = ActiveExplorer.Selection
  
  For Each messageObject In selectedItems
      Set attachmentsObject = messageObject.Attachments
  
      attachmentCount = attachmentsObject.Count
  
      While attachmentCount &gt; 0
          attachmentsObject(1).Delete
          attachmentCount = attachmentsObject.Count
      Wend
  
      messageObject.Save
  
  Next
  
  MsgBox "Attachments were removed.", vbOKOnly, "Message"
  
  Set attachmentsObject = Nothing
  Set selectedItems = Nothing
  Set messageObject = Nothing
  End Sub
  </code></pre>