0
votes

I have a small script located below that I use to download attachments from emails that contain specific words in the subject.

It worked good for a while but recently I have been experiencing intermittent issues with it not downloading the attachments.

I'm starting to think the rule is the issue and not the script.

Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
saveFolder = "path where to save file"
For Each objAtt In itm.Attachments
objAtt.SaveAsFile saveFolder & "\inventory.csv"
Set objAtt = Nothing
Next
End Sub

This is the rule:

Apply this rule after the message arrives
with CPN in the subject
  and on this computer only
run Project1.saveAtachtoDisk

You guys are my last hope as nobody else can help me.

2

2 Answers

0
votes

Replace the rule with an event handler

Private WithEvents Items As Outlook.Items 
Private Sub Items_ItemAdd(ByVal item As Object) 
  On Error Goto ErrorHandler 
  Dim Msg As Outlook.MailItem 
  If TypeName(item) = "MailItem" Then
    Set Msg = item 
    saveAttachtoDisk(Msg)
  End If
ProgramExit: 
  Exit Sub
ErrorHandler: 
  MsgBox Err.Number & " - " & Err.Description 
  Resume ProgramExit 
End Sub
0
votes

Try to add any logging statements to the VBA macro sub to make sure that the rule is triggered in Outlook. For example, you can create a text file and add log entries there.

saveFolder = "path where to save file"

What exactly folder path is specified in the code?

For Each objAtt In itm.Attachments
  objAtt.SaveAsFile saveFolder & "\inventory.csv"

It looks like you save attachments with the same name. In that case multiple attachments will be overwritten - only one file will be saved on the disk. Is that the case?