0

This is code to download attachments from mail with a certain subject from my Inbox.

I created a rule for the code to run.

How do I tweak the script to access a shared folder in the mailbox?

Public Sub SaveAttachmentsToDisk(MItem As Outlook.MailItem)

Dim oAttachment As Outlook.Attachment

Dim sSaveFolder As String

sSaveFolder = "C:\Users\DT168\Documents\outlook-attachments\"

For Each oAttachment In MItem.Attachments
    oAttachment.SaveAsFile sSaveFolder & oAttachment.DisplayName
Next

End Sub
Community
  • 1
  • 1
Libin
  • 1
  • 5
  • 1
    You are appending a backslash directly after an existing backslash? – braX Jul 13 '20 at 13:03
  • Above code is working perfectly, which is downloading from inbox. and my requirement is to download the attached file from shared outlook mailbox. – Libin Jul 13 '20 at 13:20
  • Have you tried removing one of the backslashes as @braX suggested? Perhaps like this `saveFolder = "N:\UAE\Dubai\Xfer\Libin\Log"` – Super Symmetry Jul 13 '20 at 13:53
  • 1
    Hi Dear, Can you understand my question? – Libin Jul 13 '20 at 13:59
  • @Libin The double backslash is non-fatal and appears frequently in this code. As you can see it has been a distraction that has caused you some frustration. – niton Jul 13 '20 at 17:27
  • @Libin Do you pass itm into the code through a rule? – niton Jul 13 '20 at 17:28
  • yes i have create rule for that code to run. – Libin Jul 13 '20 at 17:44
  • Given you want to run the code automatically there is the ItemAdd event. https://stackoverflow.com/questions/11263483/how-do-i-trigger-a-macro-to-run-after-a-new-mail-is-received-in-outlook You will have to add code for the subject condition and change the reference to point to the shared folder. https://stackoverflow.com/questions/9076634/get-reference-to-additional-inbox – niton Jul 13 '20 at 18:00

1 Answers1

0

Use the NameSpace.GetSharedDefaultFolder method which returns a Folder object that represents the specified default folder for the specified user. For example:

Sub ResolveName() 
 Dim myNamespace As Outlook.NameSpace 
 Dim myRecipient As Outlook.Recipient 
 Dim CalendarFolder As Outlook.Folder 
 
 Set myNamespace = Application.GetNamespace("MAPI") 
 Set myRecipient = myNamespace.CreateRecipient("Eugene Astafiev") 
 
 myRecipient.Resolve 
 If myRecipient.Resolved Then 
  Call ShowCalendar(myNamespace, myRecipient) 
 End If 
End Sub 
 
Sub ShowCalendar(myNamespace, myRecipient)  
 Dim CalendarFolder As Outlook.Folder 
 Set CalendarFolder = myNamespace.GetSharedDefaultFolder(myRecipient, olFolderCalendar)  
 CalendarFolder.Display  
End Sub
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • attached file from shared outlook mailbox should download to particular local folder in the disc drive not in outlook's folder. and downloading process is based on particular subject name. thank you for your response. – Libin Jul 13 '20 at 15:54