This is my first post here at stackoverflow and I am having an issue where my syntax is only saving email attachments if the sender's domain is not from my company's domain (my company's domain i.e. info@mycompany.com) and only saving attachments that were received from yahoo, gmail, etc. How can I edit my code so that it saves all attachements regardless of domain?
Private Sub btn_extractemails_Click()
Dim OlApp As Object
Dim OlMail As Object
Dim OlItems As Object
Dim OlFolder As Object
Dim j As Integer
Dim strFolder As String
On Error Resume Next
Set OlApp = GetObject(, "Outlook.Application")
If Err.Number = 429 Then
Set OlApp = CreateObject("Outlook.Application")
End If
strFolder = "C:\Users\p00113357\Desktop\Attaches\"
Set OlFolder = OlApp.getnamespace("MAPI").Folders("EEO").Folders("Inbox").Folders("Incoming")
Set OlItems = OlFolder.Items
For Each OlMail In OlItems
If OlMail.Attachments.Count > 0 Then
For j = 1 To OlMail.Attachments.Count
OlMail.Attachments.Item(j).SaveAsFile strFolder & "\" & OlMail.SenderEmailAddress & OlMail.Attachments.Item(j).Filename
Next j
End If
Next
Set OlFolder = Nothing
Set OlItems = Nothing
Set OlMail = Nothing
Set OlApp = Nothing
MsgBox "Done", vbInformation
End Sub
---------------------------------update------------------------------------------
I included code to show my error and when I ran it, I did not see any error codes pop up. The issue of attachments from internal email domain (i.e. info@mycompany.com) not being downloaded to my designated folder while attachments from external email domains (yahoo, gmail etc) being downloaded is still happening. Below is my attempt to obtain error codes.
Private Sub btn_extractemails_Click()
Dim OlApp As Object
Dim OlMail As Object
Dim OlItems As Object
Dim OlFolder As Object
Dim j As Integer
Dim strFolder As String
Set OlApp = GetObject(, "Outlook.Application")
If Err.Number = 429 Then
Set OlApp = CreateObject("Outlook.Application")
End If
**On Error GoTo 0
On Error Resume Next**
strFolder = "C:\Users\p00113357\Desktop\Attaches\"
Set OlFolder = OlApp.getnamespace("MAPI").Folders("EEO").Folders("Inbox")
Set OlItems = OlFolder.Items
For Each OlMail In OlItems
If OlMail.Attachments.Count > 0 Then
For j = 1 To OlMail.Attachments.Count
OlMail.Attachments.Item(j).SaveAsFile strFolder & "\" & OlMail.SenderEmailAddress & "-" & OlMail.Attachments.Item(j).Filename
Next j
End If
Next
Set OlFolder = Nothing
Set OlItems = Nothing
Set OlMail = Nothing
Set OlApp = Nothing
MsgBox "Done", vbInformation
End Sub
Thank you
---------------------------update---------------------------------------------
Following the directions in the post suggested by @notin I attempted to edit my code and it was still not working. I switched up the syntax a bit and it worked after placing lines of code in the correct order/places, thanks @notin and Josh P for your assistance with my first post. Moving forward, I will follow best practices when posting
Private Sub btn_extractemails_Click()
Dim OlApp As Object
Dim OlMail As Object
Dim OlItems As Object
Dim OlFolder As Object
Dim j As Integer
Dim strFolder As String
Set OlApp = GetObject(, "Outlook.Application")
If Err.Number = 429 Then
Set OlApp = CreateObject("Outlook.Application")
End If
On Error GoTo 0
On Error Resume Next
strFolder = "C:\Users\p00113357\Desktop\Attaches\"
Set OlFolder = OlApp.getnamespace("MAPI").Folders("EEO").Folders("Inbox")
Set OlItems = OlFolder.Items
For Each OlMail In OlItems
If OlMail.Attachments.Count > 0 Then
If OlMail.SenderEmailType = "EX" Then
For j = 1 To OlMail.Attachments.Count
OlMail.Attachments.Item(j).SaveAsFile strFolder & "\" & OlMail.Sender.GetExchangeUser().PrimarySmtpAddress & "-" & OlMail.Attachments.Item(j).Filename
Next j
End If
End If
Next
Set OlFolder = Nothing
Set OlItems = Nothing
Set OlMail = Nothing
Set OlApp = Nothing
MsgBox "Done", vbInformation
End Sub