Attachment Download

vmjan02

Well-known Member
Joined
Aug 15, 2012
Messages
1,047
Office Version
  1. 365
  2. 2021
  3. 2019
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
 
Another update, try this please.

Code:
Sub download_attachments()
  'Dim olApp As Outlook.Application, olmail As MailItem, Att As Object
  Dim olApp As Object, olmail As Object, Att As Object
  Dim olFolder As Object, sPath As String, y As Long, strfile As String
  sPath = ThisWorkbook.Path & "\"
  '
  Set olApp = CreateObject("Outlook.application")
  Set olmail = olApp.CreateItem(0)
  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 <------------- ERROR IS HERE AS 
  MsgBox "Done"
End Sub

ERROR IS IN Next. cannot save the attachments as user don not have the permission.
I am surprised.
 
Upvote 0

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Yes, now I am able to download the attachments. the problem was with my outlook, so had to ask the IT guy and ye had fixed it. So now all good and cool.
the problem as the access of mail(MIPA), now its all good

@ZVI thanks a tone for all the support. @Dante Thanks a tone to you as well.

You both rock. great guys. Will keep in touch with you both
 
Upvote 0

Forum statistics

Threads
1,213,494
Messages
6,113,972
Members
448,537
Latest member
Et_Cetera

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