Forum Discussion
VBA - Retrieve emails Attachment & Download
You can run the provided code with minor modifications to the file path.
Here is how you can do it:
- Set Up Outlook Application Object: The code already initializes the Outlook Application object, so you don't need to make any changes here.
- Authenticate and Access Mailbox: The code assumes that you have access to the mailbox of the client "Troy Corporation" and the folder "Inbox". If you have access to this mailbox, then you do not need to make any changes. However, if you need to access a different mailbox or folder, you will need to modify the code accordingly.
- Specify the Network Folder Path: You need to specify the network folder path where you want to save the attachments. Replace the placeholder "\network\folder\path" in the code with the actual path to your network folder. Make sure to use double backslashes "\" to escape the path separators.
Once you have made these changes, you can run the VBA code, and it will retrieve attachments from the specified mailbox and save them to the network folder you've specified.
Here is a step-by-step guide to running the VBA code:
Step 1: Open Excel and Access the Visual Basic for Applications (VBA) Editor
- Open Microsoft Excel.
- Press Alt + F11 to open the Visual Basic for Applications (VBA) editor.
Step 2: Insert a New Module
- In the VBA editor, go to the "Insert" menu and choose "Module." This will insert a new module into the project.
Step 3: Copy and Paste the VBA Code
- Copy the provided VBA code (the code in the previous response).
- Paste the code into the blank module you inserted in Step 2.
Step 4: Modify the File Path
- In the VBA code, locate the line that specifies the network folder path to save attachments:
strFolderPath = "\\network\folder\path\"
- Replace "\network\folder\path" with the actual path where you want to save the attachments.
Step 5: Save the Changes
- Close the VBA editor and return to your Excel workbook.
- Save your Excel workbook to ensure that the changes are preserved.
Step 6: Run the VBA Code
- Press Alt + F8 to open the "Macro" dialog box in Excel.
- In the "Macro" dialog box, you should see the "RetrieveAttachments" macro listed.
- Select the "RetrieveAttachments" macro and click the "Run" button to execute the VBA code.
That's it! The VBA code will now run and attempt to retrieve attachments from the specified email sender(s) in the Troy Corporation mailbox. Make sure you have Outlook installed and configured on your system for this code to work properly.
I am getting an error on the first code "olApp As Outlook.Application and the error 'compile error: user-defined type not defined'. I try to paste the screenshot in the reply, but the forum won't let me and I know don't why you can't to that.
Also, I have a lot read messages in the inbox, but I only want to download just the 'unread' messages. Can the code skip all the read messages and bring only the 'unread' messages and download the attachments?