1

The logic is as follows:

  • Loop through selected messages.
  • Loop through attachments of selected messages.
  • Save attachments to pre-defined folder.

I am experiencing Runtime error 13.

I'm unsure which types are mismatched.

Public Sub SaveAttachments()
    Dim objOL As Outlook.Application
    Dim objMsg As Outlook.MailItem
    Dim objAttachments As Outlook.Attachments
    Dim objSelection As Outlook.Selection
    Dim i As Long
    Dim lngCount As Long
    Set objOL = CreateObject("Outlook.Application")
    Set objSelection = objOL.ActiveExplorer.Selection
    For Each objMsg In objSelection
        Set objAttachments = objMsg.Attachments
        lngCount = objAttachments.Count
        If lngCount > 0 Then
            For i = 1 To lngCount
                If objAttachments.Item(i).Type <> 5 Then
                    objAttachments.Item(i).SaveAsFile "C:\Users\Danny\Desktop\Attachments\" & objAttachments.Item(i).FileName
                End If
            Next i
        End If
    Next objMsg
ExitSub:
        Set objAttachments = Nothing
        Set objMsg = Nothing
        Set objSelection = Nothing
        Set objOL = Nothing
End Sub
braX
  • 11,506
  • 5
  • 20
  • 33
Danny Rancher
  • 1,923
  • 3
  • 24
  • 43

1 Answers1

1

The Selection object may contain different Outlook item types - AppointmentItem, TaskItem, DocumentItem, i.e. not only the MailItem ones. So, I'd suggest declaring the item as object instead and then check out the message class or its type:

Dim individualItem As Object
For Each individualItem In Application.ActiveExplorer.Selection
    'Perform some action on individualItem
Next Message
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45