May 08 2024 05:47 AM - edited May 08 2024 05:57 AM
Hi Everyone,
I have a big task and I don't know if this can be done through VBA. Let me explain what I need to be done.
1. I have several clients that I setup an email address through Exchange server (not my personal outlook). with a password.
2. Then I have several customers that sending email attachment for that particular client. Let say one of the client’s name Troy Corporation and the email will be email address removed for privacy reasons.
The customer’s name is: Daybreak Fast, FedEx Freight, R&L Carriers, and TRN Logistics. All the customers are sending email attachment into Troy Corporation mailbox. Side Note: (sometimes I have different customer send the attached file. Let say this I have this week Mr. email address removed for privacy reasons and next week I have Mrs. email address removed for privacy reasons I need the program to check for both emails to see if one of them send me a file.
What I need is to do is login to mailbox (Troy Corporation) retrieve each email with the attached file for each customer, then download them to my network folder. If I did not receive an attachment for any of the customer to send them a second request with a note on the body "we did not receive your file" something like that.
3. So, when I login for second time to retrieve the remaining attached file. But if I did receive the email attachment either on the first or the second attempt, the customer SHOULD NOT RECEIVE a second request because I did receive their file.
I do have more after all the files are download to my network folder. But I want to know if this can be done through VBA. OR if cannot be done through VBA, which program do you think is the best to be done with?
Thank you an advanced!
May 08 2024 08:52 AM
What you are describing can be achieved using VBA, particularly if you are using Outlook as your email client. Here is a high-level overview of how you can approach this task:
Here is a simplified example of what the VBA code might look like:
Vba code is untested, please backup your file first.
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 from a specific sender
If 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
This is just a basic example to get you started. You will need to expand and customize the code to fit your specific requirements, such as handling multiple senders, sending second requests, etc. The text, steps and vba code were created with the help of AI.
My answers are voluntary and without guarantee!
Hope this will help you.
Was the answer useful? Mark as best response and Like it!
This will help all forum participants.
May 09 2024 05:39 AM
May 09 2024 10:55 PM
You can run the provided code with minor modifications to the file path.
Here is how you can do it:
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
Step 2: Insert a New Module
Step 3: Copy and Paste the VBA Code
Step 4: Modify the File Path
strFolderPath = "\\network\folder\path\"
Step 5: Save the Changes
Step 6: Run 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.
May 10 2024 06:16 AM
May 10 2024 06:41 AM
May 10 2024 08:22 AM
May 10 2024 10:11 AM - edited May 10 2024 10:12 AM
Thank you for your input sir! The VBA is 100x faster than PowerAutomate. If this task can be accomplished in vba code I rather go to that route. The gentleman told me that it can be done in vba. I just need to modify the code a little bit for my tasks. If there's no chance that cannot be done in vba, then, I will think with something else.
May 11 2024 01:12 AM
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:
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.
May 11 2024 01:26 AM
Mr. Patrick2788 idea of using Power Automate is also very interesting.
The speed of execution can depend on various factors, including the complexity of the task, the efficiency of the implementation, and the environment in which the automation is running. Comparing Power Automate to VBA in terms of speed can be nuanced.
In summary, while VBA might have certain performance advantages for specific tasks executed locally, Power Automate offers advantages in terms of ease of development, scalability, and integration capabilities. The choice between the two depends on factors such as the complexity of the task, the need for integration with external services, and the user's familiarity with each platform.
For your specific case of retrieving email attachments from an Exchange server mailbox, i think both Power Automate and VBA can accomplish the task effectively.
But in the end, you are the one who has to decide which solution is best for your project.
Here is an small outline of how you can set up a flow in Power Automate to achieve your task:
To create such a flow, follow these steps:
The steps were processed using AI.
By following these steps, you can create a Power Automate flow to automatically download attachments from emails sent by specific senders and save them to a network folder.
May 15 2024 11:45 AM
May 15 2024 05:48 PM
May 16 2024 06:24 AM
May 16 2024 08:30 AM - edited May 16 2024 08:31 AM
@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!