Download outlook attachments with vba according to subject does not work on other computers.

Angel Carvajal

New Member
Joined
Feb 22, 2022
Messages
4
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
  5. 2013
Platform
  1. Windows
I have a code that downloads attachments according to the subject, this subject is extracted line by line from excel to outlook. On my pc it works perfectly, but when checking it on another computer it doesn't throw an error message, but it doesn't download the files either.

sub download()
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 olItem In olFolder.Items
If olItem.Class = olMail Then
Set mailitem = olItem
For i = 1 To X
If InStr(1, mailitem.Subject, Cells(i, 1)) Then
Debug.Print mailitem.Subject
For Each olAtt In mailitem.Attachments
olAtt.SaveAsFile (ThisWorkbook.Sheets(1).Cells(3, 5) & "\" & olAtt.Filename)
Next olAtt
End If
Next
End If
Next olItem
End Sub

Thanx!!
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
You work with early binding, make sure the libraries are active on all pc's then.
Also the filepath is important if you use multiple pc's.

Where is the "X" variable?
 
Upvote 0
Late binding:

VBA Code:
Sub jec()
 Dim it, at As Variant, i As Long
 For Each it In CreateObject("outlook.application").GetNameSpace("MAPI").GetDefaultFolder(6).Items
    For i = 1 To Range("A" & Rows.Count).End(xlUp).Row
       If InStr(it.Subject, Cells(i, 1)) Then
         For Each at In it.attachments
            at.SaveAsFile Environ("temp") & "\" & at.DisplayName    'specify your path
         Next
       End If
    Next
 Next
End Sub
 
Upvote 0
Trabaja con enlace temprano, asegúrese de que las bibliotecas estén activas en todas las PC en ese momento.
Además, la ruta del archivo es importante si usa varias PC.

¿Dónde está la variable "X"?
Certain! I forgot to mention it, the path is a variable, but in the post I put it as seen from that variable, to avoid confusion.
On the other hand, X represents the number of rows that I am comparing, since I need to read X number of cells, by the variable i that is defined in the for loop.
 
Upvote 0
One more thing, are you sure that the macro will always run at the correct sheet?
 
Upvote 0
Late binding:

VBA Code:
Sub jec()
 Dim it, at As Variant, i As Long
 For Each it In CreateObject("outlook.application").GetNameSpace("MAPI").GetDefaultFolder(6).Items
    For i = 1 To Range("A" & Rows.Count).End(xlUp).Row
       If InStr(it.Subject, Cells(i, 1)) Then
         For Each at In it.attachments
            at.SaveAsFile Environ("temp") & "\" & at.DisplayName    'specify your path
         Next
       End If
    Next
 Next
End Sub
This code works on my computer, not on my colleagues :( .
 
Upvote 0
How did you specify your path?
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,198
Members
452,617
Latest member
Narendra Babu D

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top