Outlook script used to download attachments stopped working

巧了我就是萌 提交于 2019-12-12 06:18:42

问题


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.


回答1:


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



回答2:


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?



来源:https://stackoverflow.com/questions/29568612/outlook-script-used-to-download-attachments-stopped-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!