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

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
Harati
  • 1
  • 3
  • You should find it easier to find errors such as the missing `rvFolder` if you use`OptionExplicit`. – niton Nov 17 '22 at 16:35

1 Answers1

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
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • 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` – Harati Nov 17 '22 at 13:18
  • You need to define objects in the code if you didn't so. – Eugene Astafiev Nov 17 '22 at 13:26
  • `Dim rvInbox as Outlook.Folder` – Eugene Astafiev Nov 17 '22 at 13:27
  • 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` – Harati Nov 17 '22 at 13:36
  • You don't need to use the Items property on the Inbox folder. – Eugene Astafiev Nov 17 '22 at 13:46
  • I've updated the post, so you may see where the problem is. – Eugene Astafiev Nov 17 '22 at 13:49
  • I'd suggest posting a new question in a separate thread, so readers could recognize questions and answers easily. – Eugene Astafiev Nov 17 '22 at 16:21
  • I still get this Runtime error in Set rvItems = rvFolder.Folders("Test").Items line.... Does it matters that i am using Outlook 365? – Harati Nov 22 '22 at 07:21
  • Are you sure that such folder exists in the folder tree in your Outlook 365? – Eugene Astafiev Nov 22 '22 at 07:41
  • Yes, of course : I don't know how can i upload a pic of my Inbox tree to see what i'm trying to achieve, but yes - i have it under Inbox. – Harati Nov 22 '22 at 07:54
  • Try to iterate over all subfolders and check their names. Following that way you can get the required instance. – Eugene Astafiev Nov 22 '22 at 08:13
  • It's done couple of times but still no effect. Did the name of the sub folder matters like this : now i have "Meteologica SA Power Forecast" as source folder, but if it's like this : "MeteologicaSAPowerForecast"? I mean without the spaces? – Harati Nov 22 '22 at 08:41