Attachment Download

vmjan02

Well-known Member
Joined
Aug 15, 2012
Messages
1,134
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
  5. 2013
I have the below code and have it is on Outlook VBA on ThisOutlookSession

But I am still not able to download the attachments in the folder, I am trying this for quite a while now but no luck, did
google as well. It is not showing any error, but still not downloading the attachment in the folder

Help please.

Code:
Public WithEvents objInboxItems As Outlook.Items
Private Sub Application_Startup()
   Set objInboxItems = Session.GetDefaultFolder(olFolderInbox).Items
End Sub
Private Sub objInboxItems_ItemAdd(ByVal Item As Object)
   Dim objMail As Outlook.MailItem
   Dim strSenderAddress As String
   Dim strSenderDomain As String
   Dim objAttachment As Attachment
   Dim strFolderPath As String
   Dim strFileName As String


   If Item.Class = olMail Then
      Set objMail = Item


      'Get sender domain
      strSenderAddress = objMail.SenderEmailAddress
      strSenderDomain = Right(strSenderAddress, Len(strSenderAddress) - InStr(strSenderAddress, "@"))


      'Change to the specific domain as per your needs
      If strSenderDomain = "vs@gmail.com" Then
         If objMail.Attachments.Count > 0 Then
            For Each objAttachment In objMail.Attachments


                strFolderPath = "E:\Performance Report\"
                strFileName = objMail.Subject & " " & Chr(45) & " " & objAttachment.FileName
                objAttachment.SaveAsFile strFolderPath & strFileName
            Next
         End If
      End If
   End If
End Sub
 
Upvote 0

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
did removed the code "on Error Resume Next"
There is no error at all it just it asked for the folder selection and then msgbox "Done"

Using Outlook 2010. and there is no subfolder its in the inbox folder
 
Last edited:
Upvote 0
The code requirs full Outlook's path from the root to the Folder with subfolders.
Could you post it?
If it is a common folder then code may not work, it depends on server configuration
 
Last edited:
Upvote 0
And please do you have 1 or more accounts?
 
Upvote 0
did removed the code "on Error Resume Next"
There is no error at all it just it asked for the folder selection and then msgbox "Done"
Using Outlook 2010. and there is no subfolder its in the inbox folder


Try this please:

Run the macro and select the folder that contains the emails.

Code:
Sub download_attachments()
  Dim olApp As Outlook.Application, olmail As MailItem, Att As Object
  Dim olFolder As Outlook.Folder, sPath As String, y As Long, strfile As String
  sPath = ThisWorkbook.Path & "\"
  '
  Set olApp = CreateObject("Outlook.application")
  Set olmail = olApp.CreateItem(olMailItem)
  Set olFolder = olApp.GetNamespace("MAPI").PickFolder
  '
  For Each olmail In olFolder.Items
    If TypeName(olmail) = "MailItem" Then
      y = 1
      For Each Att In olmail.Attachments
        strfile = sPath & olmail.Attachments.Item(y).Filename
        olmail.Attachments.Item(y).SaveAsFile strfile
        y = y + 1
      Next Att
    End If
  Next
  MsgBox "Done"
End Sub
 
Upvote 0
Well, does the below code work?
Rich (BB code):
Option Explicit

Public WithEvents objInboxItems As Outlook.Items

' Run this code manually for the testing or reload Outlook
Private Sub Application_Startup()
   Const GmailAccountName = "...@gmail.com" ' <-- put gmail root folder name (actually it's gmail email)
   Set objInboxItems = Application.GetNamespace("MAPI").Folders(GmailAccountName).Folders("Inbox").Items
End Sub

Private Sub objInboxItems_ItemAdd(ByVal Item As Object)
  MsgBox "Ok"
  ' Your code is here
  '...
End Sub
 
Last edited:
Upvote 0
The below code works with any input emails independently of input folder:
Rich (BB code):
' Code of ThisOutlookSession
Option Explicit

Private Sub Application_Startup()
' Do nothing, but this tries to enable macros or asks to confirm it
End Sub

Private Sub Application_NewMailEx(ByVal EntryIDCollection As String)
  
  Dim objItem As Object
  Dim objMail As MailItem
  
  Set objItem = Application.GetNamespace("MAPI").GetItemFromID(EntryIDCollection)
  If objItem.Class <> olMail Then Exit Sub
  Set objMail = objItem
  
  ' This is just for the testing
  MsgBox "Message subject: " & objMail.Subject & vbLf _
       & "Message sender: " & objMail.SenderName & " (" & objMail.SenderEmailAddress & ")"
  
  ' Your code goes here
  ' ...
  
  
  ' Release memory of the object variables
  Set objItem = Nothing
  Set objMail = Nothing
  
End Sub
 
Last edited:
Upvote 0
Does code from post 17 or 18 work?
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,160
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top