Link to home
Start Free TrialLog in
Avatar of Brian Hansen
Brian Hansen

asked on

Outlook VBA worked in 2013 but not in 2016

I have the below code that worked just fine in Outlook 2013, but when given a different laptop with 2016 the code does not work. When I fun this code it appears that it isn't recognizing attachments correctly, every time I run it I get the "I didn't find any Web App files in your mail." There are in fact valid files that should be recognized and moved.
I am a complete novice, I did not write the code, it was found via google, modified for my needs, so please be easy on me! :)

Option Explicit
Sub MoveFiles()

'Find specific files in email and move to appropriate folder
On Error GoTo GetAttachments_err
    Dim appOl As New Outlook.Application
    Dim ns As Outlook.NameSpace
    Dim Inbox As Outlook.MAPIFolder
    Dim item As Object
    Dim Atmt As Outlook.Attachment
    Dim FileName As String
    Dim i As Integer
    Dim attCount As Long
    Dim strFile As String
    Dim sFileType As String
    Set ns = appOl.GetNamespace("MAPI")
    Set Inbox = ns.GetDefaultFolder(olFolderInbox)
    i = 0
' Check Inbox for messages and exit of none found
    If Inbox.Items.Count = 0 Then
        MsgBox "There are no messages in the Inbox.", vbInformation, _
               "Nothing Found"
        Exit Sub
    End If
' Check each message for attachments
    For Each item In Inbox.Items
For Each Atmt In item.Attachments
    If LCase(Right(Atmt.FileName, 4)) = ".csv" Then
        If LCase(Left(Atmt.FileName, 3)) = "pdf" Then
            Atmt.SaveAsFile "C:\Web Unload\PDF Unload\" & Atmt.FileName
        ElseIf LCase(Left(Atmt.FileName, 3)) = "unl" Then
            Atmt.SaveAsFile "C:\Web Unload\Unload Report\" & Atmt.FileName
        End If
        i = i + 1
    item.Delete
    End If
             Next Atmt
    Next item

    If i > 0 Then
        MsgBox i & " files were exported and saved." _
        '& vbCrLf & "I have saved them into the C:\Users\brian_hansen\Desktop\Web Unload." _
        '& vbCrLf & vbCrLf & "Have a nice day.", vbInformation, "Finished!"
    Else
        MsgBox "I didn't find any Web App files in your mail.", vbInformation, "Finished!"
    End If
' Clear memory
GetAttachments_exit:
    Set Atmt = Nothing
    Set item = Nothing
    Set ns = Nothing
    Set appOl = Nothing
    Exit Sub
' Handle errors
GetAttachments_err:
    MsgBox "An unexpected error has occurred." _
        & vbCrLf & "Please note and report the following information." _
        & vbCrLf & "Macro Name: GetAttachments" _
        & vbCrLf & "Error Number: " & Err.Number _
        & vbCrLf & "Error Description: " & Err.Description _
        , vbCritical, "Error!"
    Resume GetAttachments_exit
End Sub

Open in new window

Avatar of Bill Prew
Bill Prew

I tested your code here in my Outlook 2016 (64 bit) install and it worked fine.  I created one test email with an attachment of pdf001.csv and it found it, saved the attachment to disk, deleted the email, and reported the statistics in the proper MsgBox.

So I don't think it's a code problem.  Are you sure it's looking in the proper Inbox, do you have multiple accounts or folders in Outlook, it will look at the default one as it's coded.  Are you sure there are emails that meet the criteria for an attachment to be processed - I know, routine question, but I have to ask.

If nothing obvious jumps out at you then I can talk you through some interactive debugging of the module to try and learn more.

~bp
Avatar of Brian Hansen

ASKER

Yes, there are actually 5 different Inbox's, two personal and 3 shared. I was wondering if that was the issue. Is there a way in the code to point to a specific inbox?
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
BINGO!! Thank you so much for your assistance!
Great, glad that was helpful.

~bp
Given some of the changes to the site, if you have any questions about how to close a question take a look at this article, it should help you with the question close process.


For future reference here some other items related to closing questions in case they are helpful.


And don't forget you can always "request attention" on a question if you are not sure how to proceed and a support person will be in touch to help you out.


~bp