The code reads the lines from Excel in column "A", looks for them in Outlook and downloads the attached file to the folder that is in range("E3"), which changes depending on the computer.
But, on my computer it works perfectly, on my colleagues' computer it doesn't download the files.
Sub Descarga()
Dim it, at As Variant, t As Long
Dim olApp As Outlook.Application
Dim olNS As Outlook.Namespace
Dim olFolder As Outlook.MAPIFolder
Dim olItem As Object
Dim mailitem As Outlook.mailitem
Dim olAtt As Outlook.Attachment
Set olApp = New Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
Set olFolder = olNS.GetDefaultFolder(olFolderInbox)
For Each it In CreateObject("outlook.application").GetNamespace("MAPI").GetDefaultFolder(6).Items
For t = 1 To Range("A" & Rows.Count).End(xlUp).Row
If InStr(it.Subject, Cells(t, 1)) Then
For Each at In it.Attachments
at.SaveAsFile (Range("E3")) & "\" & at.DisplayName 'Range("E3") is the path
Next
End If
Next
Next
End Sub
CodePudding user response:
Firstly, do not use at.DisplayName
- use at.FileName
.
Secondly, check the attachment type before saving - OOM won't let you save attachments that are, for example, embedded OLE objects, you need to check the attachment type first.
For Each at In it.Attachments
if at.Type = 1 Then 'olByValue
at.SaveAsFile (Range("E3")) & "\" & at.FileName 'Range("E3") is the path
End If
Next
CodePudding user response:
First of all, there is no need to create a new Outlook Application
class for each item in a folder (even if the new instance is not created, it makes sense to break the long chain of property and method calls for your convenience and clean code):
For Each it In CreateObject("outlook.application").GetNamespace("MAPI").GetDefaultFolder(6).Items
Instead, use the Find
/FindNext
or Restrict
methods to find items that correspond to your conditions with the subject line. Read more about them in the following articles:
- How To: Use Find and FindNext methods to retrieve Outlook mail items from a folder (C#, VB.NET)
- How To: Use Restrict method to retrieve Outlook mail items from a folder
If you need to process items from different folder you may also consider using the AdvancedSearch
method of the Application
class. Read more about this method in the Advanced search in Outlook programmatically: C#, VB.NET article. The key benefits of using the AdvancedSearch
method in Outlook are:
- The search is performed in another thread. You don’t need to run another thread manually since the
AdvancedSearch
method runs it automatically in the background. - Possibility to search for any item types: mail, appointment, calendar, notes etc. in any location, i.e. beyond the scope of a certain folder. The
Restrict
andFind
/FindNext
methods can be applied to a particularItems
collection (see theItems
property of theFolder
class in Outlook). - Full support for DASL queries (custom properties can be used for searching too). You can read more about this in the Filtering article in MSDN. To improve the search performance, Instant Search keywords can be used if Instant Search is enabled for the store (see the
IsInstantSearchEnabled
property of theStore
class). - You can stop the search process at any moment using the
Stop
method of theSearch
class.
Finally, I'd recommend making sure the file path passed to the SaveAsFile
method is valid and doesn't contain forbidden symbols:
at.SaveAsFile (Range("E3")) & "\" & at.DisplayName 'Range("E3") is the path