2015-03-31 61 views
-2

我需要根据发送给哪个电子邮件(而不是发件人)自动保存附件。Outlook 2013代码根据发送到的电子邮件来保存附件

我在邮件服务器pdf @,xml @,txt @上有3封电子邮件。如果电子邮件发送到@pdf,我需要将它保存在网络驱动器上,其他电子邮件也一样,但不同的位置。

我见过的所有其他代码只考虑了发件人没有发送到的地址。

回答

0

在Outlook中创建了3个列表和一个规则。

当电子邮件发送到(添加所有后期列表)并且有附件 运行此脚本。 PS。您必须编辑所有路径,文件夹名称和postlistnames。

Sub SaveAllAttachments(objitem As MailItem) 

Dim objAttachments As Outlook.Attachments 
Dim strName, strLocation As String 
Dim dblCount, dblLoop As Double 

Dim strSub As String 
Dim iRcpCount, iRcp As Integer 

strLocation = "O:\PDF\" 

On Error GoTo ExitSub 
If objitem.Class = olMail Then 
    Set objAttachments = objitem.Attachments 
    dblCount = objAttachments.Count 
    If dblCount <= 0 Then 
     GoTo 100 
    End If 

    strSub = "" 
    iRcpCount = objitem.Recipients.Count 
    For iRcp = 1 To iRcpCount 
     If objitem.Recipients(iRcp).Name = "Postlist1" Then 
      strSub = "Folder1onOdrive" 
     ElseIf objitem.Recipients(iRcp).Name = "Postlist2" Then 
      strSub = "Folder2onOdrive" 
     ElseIf objitem.Recipients(iRcp).Name = "Postlist3" Then 
      strSub = "Folder3onOdrive" 
     End If 

    Next iRcp 

    For dblLoop = 1 To dblCount 
     strName = objAttachments.Item(dblLoop).FileName 
     'strName = strLocation & strName 
     strName = strLocation & strSub & strName 
     'strName = strLocation & strName 
     objAttachments.Item(dblLoop).SaveAsFile strName 
    Next dblLoop 
    objitem.Delete 
End If 
100 
ExitSub: 
Set objAttachments = Nothing 
Set objOutlook = Nothing 
End Sub 
0

您可以处理应用程序类的ItemSend事件,您可以在其中检出收件人地址(或收件人收藏夹)并在需要时保存附件。例如:

Public WithEvents myOlApp As Outlook.Application 

Public Sub Initialize_handler() 
    Set myOlApp = Outlook.Application 
End Sub 

Private Sub myOlApp_ItemSend(ByVal Item As Object, Cancel As Boolean) 
    Dim prompt As String 
    prompt = "Are you sure you want to send " & Item.Subject & "?" 
    If MsgBox(prompt, vbYesNo + vbQuestion, "Sample") = vbNo Then 
    Cancel = True 
    End If 
End Sub 

的ItemSend事件通过督察发射每当微软Outlook项目被发送,或者由用户(检查员关闭之前,但用户后,点击发送按钮),或者发送Outlook项目的方法(如MailItem)在程序中使用。

您可能会发现Getting Started with VBA in Outlook 2010文章有帮助。

相关问题