逻辑如下:
我遇到运行时错误13。
我不确定哪些类型不匹配。
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
Selection
对象可能包含不同的Outlook项目类型-AppointmentItem
,TaskItem
,DocumentItem
,即不仅是MailItem
。因此,我建议改为将项目声明为object
,然后检查消息类或其类型:
Dim individualItem As Object
For Each individualItem In Application.ActiveExplorer.Selection
'Perform some action on individualItem
Next Message