Outlook VBA宏上的运行时错误'13'类型不匹配

时间:2018-08-27 23:07:18

标签: vba outlook outlook-vba

我使用某些VBA代码将电子邮件附件保存到共享驱动器时遇到问题。当我为大量附件运行宏时,出现运行时错误'13'-键入不匹配。

这是我使用的从另一篇文章中提取的代码:

Public Sub SaveOLFolderAttachments()

' Ask the user to select a file system folder for saving the attachments
Dim oShell As Object
Set oShell = CreateObject("Shell.Application")
Dim fsSaveFolder As Object
Set fsSaveFolder = oShell.BrowseForFolder(0, "Please Select a Save Folder:", 1)
If fsSaveFolder Is Nothing Then Exit Sub
' Note:  BrowseForFolder doesn't add a trailing slash

' Ask the user to select an Outlook folder to process
Dim olPurgeFolder As Outlook.MAPIFolder
Set olPurgeFolder = Outlook.GetNamespace("MAPI").PickFolder
If olPurgeFolder Is Nothing Then Exit Sub

' Iteration variables
Dim msg As Outlook.MailItem
Dim att As Outlook.Attachment
Dim sSavePathFS As String
Dim sDelAtts

For Each msg In olPurgeFolder.Items

  sDelAtts = ""

  ' check each msg for attachments
  If msg.Attachments.Count > 0 Then
    While msg.Attachments.Count > 0

      ' Save the file
      sSavePathFS = fsSaveFolder.Self.Path & "\" & msg.Attachments(1).FileName
      msg.Attachments(1).SaveAsFile sSavePathFS
      If msg.BodyFormat <> olFormatHTML Then
          sDelAtts = sDelAtts & vbCrLf & "<file://" & sSavePathFS & ">"
      Else
          sDelAtts = sDelAtts & "<br>" & "<a href='file://" & sSavePathFS & "'>" & sSavePathFS & "</a>"
      End If

      ' Delete the current attachment.

      msg.Attachments(1).Delete

    Wend

    ' Modify the body of the msg to show the file system location of the deleted attachments.
    If msg.BodyFormat <> olFormatHTML Then
      msg.Body = msg.Body & vbCrLf & vbCrLf & "Attachments Deleted:  " & Date & " " & Time & vbCrLf & vbCrLf & "Saved To:  " & vbCrLf & sDelAtts
    Else
      msg.HTMLBody = msg.HTMLBody & "<p></p><p>" & "Attachments Deleted:  " & Date & " " & Time & vbCrLf & vbCrLf & "Saved To:  " & vbCrLf & sDelAtts & "</p>"
    End If

    ' Save the edits to the msg.
    msg.Save

  End If

Next

End Sub

当我进入代码时,它会在代码末尾的“下一步”处停止并显示上述错误。

关于我为什么会在Next上出现类型不匹配错误的任何想法?这里有人可以帮助我解决此问题吗?任何帮助表示赞赏。

0 个答案:

没有答案