0

I started to create a code to download specific attachments to a disk folder, but i have a Run-time error 424 somewhere in the Set rvItems = rvFolder.Folders("Inbox").Items Set rvItems = rvFolder.Folders("Test").Items part. I don't know for what reason and can't figure it out. I am still not declared which exactly attachments i need to be downloaded, but want to see if it works at first place. Can you check it-out and tell me where i'm wrong/missing something, please? Here's the code :

'ThisOutlookSession part

Private WithEvents rvItems As Outlook.Items

Private Sub Application_Startup()

Dim rvApp As Outlook.Application
Dim rvNS As Outlook.NameSpace

Set rvApp = Outlook.Application
Set rvNS = rvApp.GetNamespace("MAPI")
Set rvInbox = rvNS.GetDefaultFolder(olFolderInbox).Items
Set rvItems = rvFolder.Folders("Inbox").Items
Set rvItems = rvFolder.Folders("Test").Items

End Sub


'Modules part
Private Sub rvItems_ItemAdd(ByVal item As Object)

Dim rvMail As Outlook.mailitem
Dim rvAtt As Outlook.Attachment

If TypeName(item) = "MailItem" Then

Set rvMail = item
   
 For Each rvAtt In rvMail.Attachments
    rvAtt.SaveAsFile "C:\Users\BG-TRADE-005\OneDrive - alpiq.com\Desktop\Schedule\Mail_Temp \Download" & rvAtt.FileName
  Next rvAtt

  Set rvMail = Nothing

End If

End Sub

1
  • You should find it easier to find errors such as the missing rvFolder if you useOptionExplicit. Commented Nov 17, 2022 at 16:35

1 Answer 1

0

In the code you are trying to assign an Outlook type instead of using the Application property:

Set rvApp = Outlook.Application

So, the code should re-use the global Application property available in the Outlook VBA environment.

Private WithEvents rvItems As Outlook.Items

Private Sub Application_Startup()

Dim rvNS As Outlook.NameSpace
Dim rvInbox as Outlook.Folder

Set rvNS = Application.GetNamespace("MAPI")
Set rvInbox = Application.GetDefaultFolder(olFolderInbox)
Set rvItems = rvFolder.Folders("Test").Items

End Sub


'Modules part
Private Sub rvItems_ItemAdd(ByVal item As Object)

Dim rvMail As Outlook.mailitem
Dim rvAtt As Outlook.Attachment

If TypeName(item) = "MailItem" Then

Set rvMail = item
   
 For Each rvAtt In rvMail.Attachments
    rvAtt.SaveAsFile "C:\Users\BG-TRADE-005\OneDrive - alpiq.com\Desktop\Schedule\Mail_Temp \Download" & rvAtt.FileName
  Next rvAtt

  Set rvMail = Nothing

End If

End Sub

If the problem still persists and the code can't locate the Test folder I'd suggest iterating over all subfolders and checking the Name property. Following that way you can find the required folder without errors.

Also you need to pay attention to the fact that in the code Items and Folder instances are mixed:

Set rvInbox = rvNS.GetDefaultFolder(olFolderInbox).Items
Set rvItems = rvFolder.Folders("Inbox").Items
Set rvItems = rvFolder.Folders("Test").Items

You are trying to get a subfolder on the Items instance where such property doesn't exists.

If you need to get a subfolder of the Inbox folder you need to use the following code instead:

Set rvInbox = rvNS.GetDefaultFolder(olFolderInbox)
Set rvItems = rvFolder.Folders("Test").Items
Sign up to request clarification or add additional context in comments.

12 Comments

Thank you Eugene. I just made the changes, but now the code tells me "Run-time error 438 Object doesn't support this property or method for the line Set rvItems = rvFolder.Folders("Inbox").Items
You need to define objects in the code if you didn't so.
Dim rvInbox as Outlook.Folder
i added this line, deleted a space in rvAtt.SaveAsFile "C:\Users\BG-TRADE-005\OneDrive - alpiq.com\Desktop\Schedule\Mail_Temp \Download" & rvAtt.FileName line, but still have yellow line at Set rvInbox = Application.GetDefaultFolder(olFolderInbox).Items
You don't need to use the Items property on the Inbox folder.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.