Forum Discussion
VBA - Retrieve emails Attachment & Download
Thank you for the great start. As you mentioned that I need to create an instance of the outlook application object. Since I am new to VBA code and I don't know that much about it. I was hoping to give me something from start to finish so all I need to do is to replace a minor code such the path of the file and run the VBA code.
Question: Can I run your code as is by changing the file path?
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.
- TTC-BlueHill1May 10, 2024Copper ContributorGood morning @NikoninoDE
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? - TTC-BlueHill1May 10, 2024Copper ContributorAlso to enter to the client mailbox. I use the user name which is Troy email address and the password.
I don't see where in the code to enter both user name and the password.- NikolinoDEMay 11, 2024Gold Contributor
It seems like you are encountering a couple of issues. Let us address them step by step:
1. 'User-defined type not defined' error: This error typically occurs when the necessary Outlook object library reference is not added to your VBA project. To resolve this, you need to ensure that the Microsoft Outlook Object Library is referenced in your VBA project. Here's how you can do it:
- In the VBA editor, go to the "Tools" menu and select "References."
- In the References dialog box, find and check "Microsoft Outlook xx.x Object Library" (where xx.x represents the version number).
- Click "OK" to save the changes.
After adding the reference, the 'Outlook' objects should be recognized without errors.
2. Downloading only unread messages: To modify the code to download only unread messages, you can add a condition to check if the email is unread before processing it. Here's how you can do it:
Vba code is untested please backup your file
Sub RetrieveAttachments() Dim olApp As Outlook.Application Dim olNs As Outlook.NameSpace Dim olFolder As Outlook.MAPIFolder Dim olMail As Outlook.MailItem Dim olAttachment As Outlook.Attachment Dim strFolderPath As String ' Initialize Outlook Application Set olApp = New Outlook.Application Set olNs = olApp.GetNamespace("MAPI") ' Access the client's mailbox folder Set olFolder = olNs.Folders("Troy Corporation").Folders("Inbox") ' Specify the network folder path to save attachments strFolderPath = "\\network\folder\path\" ' Loop through each email in the folder For Each olMail In olFolder.Items ' Check if the email is unread and from a specific sender If olMail.UnRead And olMail.SenderEmailAddress = "email address removed for privacy reasons" Then ' Loop through attachments and save them to the network folder For Each olAttachment In olMail.Attachments olAttachment.SaveAsFile strFolderPath & olAttachment.FileName Next olAttachment End If Next olMail ' Cleanup Set olAttachment = Nothing Set olMail = Nothing Set olFolder = Nothing Set olNs = Nothing Set olApp = Nothing End Sub
With this modification, the code will only process unread emails from the specified sender and download their attachments to the specified network folder. Make sure to replace "email address removed for privacy reasons" with the actual sender's email address.
3. Specifying username and password: If you are accessing an Exchange server mailbox using VBA, typically, it will use the credentials of the currently logged-in user. However, if you need to specify a different username and password, you might need to use different methods, such as Exchange Web Services (EWS) or Outlook REST API, which would involve more complex authentication mechanisms. For simplicity, the provided code assumes access using the currently logged-in user's credentials. The text, steps and code were created with the help of AI.
- TTC-BlueHill1May 16, 2024Copper Contributor
NikolinoDE
I have the Microsoft Outlook 16.0 Object Library check in. But when I try to run the code, I got an error 'Run-time error '-2147221233 (8004010f)'" The attempted operation failed. An object could not be found'. Please let me know how I can fixed this so I rerun the code again.
Thanks again!