• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 406
  • Last Modified:

Outlook VBA Save PDF's Error

The following code saves the attachments to a folder for all of the selected emails within a folder from outlook. This code works great except for the fact that it will only save around 200 emails worth. Once the code runs and gets to around 200 it errors and quits.

Was wondering if there is limit of emails that can be selected and processed through the VBA, or is there a setting somewhere that can be changed.

Public Sub SaveSelectedAttachments()
On Error GoTo ErrorHandler
Dim olns As Outlook.NameSpace
Dim objMsg As Object
Dim objAttachments As Outlook.Attachments
Dim objSelectedItems As Outlook.Selection
Dim f, i, j, k, counter As Integer
Dim attPath, attFileName As String, strDesktop As String, strR As String
Set olns = Application.GetNamespace("MAPI")
Set objSelectedItems = olns.Application.ActiveExplorer.Selection

strDesktop = "C:\Users\Jbryan\Desktop\SavedPDFS"
msgfolder = strDesktop

For Each objMsg In objSelectedItems
    k = k + 1
    j = 0
    If objMsg.Class = olMail Then
      Set objAttachments = objMsg.Attachments
        counter = objAttachments.Count
      If counter > 0 Then
        With objMsg
            strR = Format(.ReceivedTime, "Short Date")
        End With
        strR = Replace(strR, "/", "-", 1)
                        
            For i = objAttachments.Count To 1 Step -1
                j = j + 1
                f = f + 1
                iExt = Len(objAttachments.Item(i).FileName) - InStrRev(objAttachments.Item(i).FileName, ".")
                strE = Right(objAttachments.Item(i).FileName, iExt)
                attFileName = Left(objAttachments.Item(i).FileName, Len(objAttachments.Item(i).FileName) - iExt - 1)
                attFileName = strR & "_" & k & "_" & j & ".pdf"
                attPath = msgfolder & "\" & attFileName
                objAttachments.Item(i).SaveAsFile attPath
            Next i

        End If
    End If

  Next objMsg
ExitSub:
  Set objAttachments = Nothing
  Set objMsg = Nothing
  Set objSelectedItems = Nothing
  Set olns = Nothing
  MsgBox "Attachments have been successfully saved."
Exit Sub

ErrorHandler:
 MsgBox "Saveattachments( ) Subroutine" & vbCrLf & vbCrLf & "Error Code: " & Err.Number & vbCrLf & Err.Description
  
  Err.Clear
  GoTo ExitSub
End Sub

Open in new window

0
jb702
Asked:
jb702
  • 7
  • 5
1 Solution
 
Robberbaron (robr)Commented:
what exact error ?  what line throw it ?

unless u have a lot of attachments and counter f is overflowing ?   try dimming as long. wont slow code much.
0
 
Robberbaron (robr)Commented:
'more error handling
MsgBox "Saveattachments( ) Subroutine" & vbCrLf & vbCrLf & "Error Code: " & Err.Number & vbCrLf & Err.Description & vbCrlf & "Failed on message#" & str(k) & " --- Attachment" & str(j)
0
 
jb702Author Commented:
this is the error i got before i redimmed f to long my code processed 183 attachments.
then after re-dim of f to long it processed 182 attachments of 332 total available attachments to save.

The folder is on my desktop, and i am renaming the "eReceipt.pdf" before i save it so there is no duplicate issue. this is weird.


Error snapshot
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
jb702Author Commented:
I changed the error handler as suggested and this is the message result.

code saved 171 attachments of a possible 426




Revised Error
0
 
jb702Author Commented:
Decided to run another batch this time with 262 available attachments.  Here is the error message.  This time it saved 197 attachments.

Error3
0
 
jb702Author Commented:
I wanted to see if it would error on the same message again, so i selected the same 126 emails with a total of 198 attachments and low and behold it did error out on the same message and same attachment as previous post. But there is nothing different with this attachment. It is just like all the others.
0
 
jb702Author Commented:
commented out the Error Handling code and ran same set of emails, and code broke at this line.

objAttachments.Item(i).SaveAsFile attPath

with the same error.
0
 
Robberbaron (robr)Commented:
the error code being negative suggests it is an error within Outlook.... and probably with the attachment ?  there is an attachment type property that may be worth checking.
http://msdn.microsoft.com/en-us/library/office/ff868693(v=office.15).aspx


is it the same message each time ?

alternative is to log the error and keep processing the rest of the messages.  You could do this by setting a flag or catagory on the offending msg.

see http://msdn.microsoft.com/en-us/library/office/dn320330(v=office.15).aspx
Categories, Sensitivity or FlagRequest properties as you can later sort by these.
0
 
jb702Author Commented:
Ok, so here is what i did next, i ran the code again with the same set of emails except i stopped short of the email that was creating the error. My code ran fine, saved 182 attachments.

then i ran the code on the next set of emails which included the email that was causing the error. The code ran fine saved 80 attachments including the attachments that were in the email that was causing the error above.

Any ideas now?
0
 
Robberbaron (robr)Commented:
No....

the err number still suggests an issue with outlook.

but only alternate is that these filenames were duplicate but that seems unlikley.

if you are able to do more testing, try using Timer function to force a uniquie id

attFileName = strR & "_" & Format(k,"0000") & "_" & Format(Timer*10,"00000") & ".pdf"
DoEvents

Open in new window


note i convert all the numbers to strings using format function rather than let VBA 'cast' the number to variant then to string.  keeps names consistent as well.

the DoEvents is there in case it is a timing issue with saving.
0
 
jb702Author Commented:
I never resolved,please close question.
0
 
Robberbaron (robr)Commented:
its up to you to close question...
0
 
Martin LissRetired ProgrammerCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now