Outlook Macro to Save Attachments to Network Drive



  • I have tried this code but getting a 438 error. This code is a portion of what I have in an effort to troubleshoot. The code should pick out and email on arrival with a particular email address and subject.Then I would like it to save to a network drive under a particular name overriding the file daily when the automated report is received. This code is located under the ThisOutlookSession. I have Microsoft Office 16 Object Library selected. Not sure why this is not working. Does anyone see what I am doing wrong?

    Code Below:

    Private WithEvents Items As Outlook.Items
    
    Private Sub Application_Startup()
        Dim olApp As Outlook.Application
        Dim objNS As Outlook.NameSpace
        Set olApp = Outlook.Application
        Set objNS = olApp.GetNamespace("MAPI")
        Set Items = objNS.GetDefaultFolder(olFolderInbox).Items
    End Sub
    
    Private Sub Items_ItemAdd(ByVal item As Object)
    
    On Error GoTo ErrorHandler
    
        'Only act if it's a MailItem
        Dim Msg As Outlook.MailItem
        If TypeName(item) = "MailItem" Then
            Set Msg = item
    
        'Change variables to match need. Comment or delete any part unnecessary.
            If (Msg.SenderEmailAddress = "[email protected]") And _
            (Msg.Subject = "Last 7 Day House Ads") And _
            (Msg.Attachments.Count >= 1) Then
            
        'Set folder to save in.
        Dim olDestFldr As Outlook.MAPIFolder
        Dim myAttachments As Outlook.Attachments
        Dim Att As String
            
        'location to save in.  Can be root drive or mapped network drive.
        Const attPath As String = "T:\1 Daily Report Raw Data\"
           
           
        ' save attachment
       Set myAttachments = item.Attachments
        Att = myAttachments.item(1).House_Report
        myAttachments.item(1).SaveAsFile attPath & Att
            
        ' mark as read
       Msg.UnRead = False
    End If
    End If
        
    
    ProgramExit:
      Exit Sub
      
    ErrorHandler:
      MsgBox Err.Number & " - " & Err.Description
      Resume ProgramExit
    End Sub
    


  • @cturley I found the issue. The problem is on the ATT- line where it originally ended in Display name and I changed it to the name I wanted the file's name to be. This does not work. When I changed it, it worked saving the file by its original name. I changed the code to get the outcome I needed.


  • administrators

    Thanks for sharing!


Log in to reply
 

Looks like your connection to Codingislove Forum was lost, please wait while we try to reconnect.