0

因此,目标是当我收到来自客户的包含所需附件的电子邮件时,将附件保存到我选择的位置。

这是我的新代码,它编译但不输出文件?

提前致谢。

Private WithEvents Items As Outlook.Items

Private Sub Application_Startup()
    Dim olApp As Outlook.Application
    Dim objNS As Outlook.NameSpace
    Set olApp = Outlook.Application
    Set objNS = olApp.GetNamespace("MAPI")
    Set Items = objNS.GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub Application_NewMail()

Dim oInbox As MAPIFolder
Dim oItem As MailItem

Set oInbox = Application.Session.GetDefaultFolder(olFolderInbox)
Set oItem = oInbox.Items.GetLast

'Only act if it's a MailItem
Dim Msg As Outlook.MailItem
If TypeName(item) = "MailItem" Then
    Set Msg = oItem

    'Change variables to match need. Comment or delete any part unnecessary.
    If (Msg.SenderName = "Name Of Person") And _
        (Msg.Subject = "Subject to Find") And _
        (Msg.Attachments.Count >= 1) Then

        'Set folder to save in.
        Dim olDestFldr As Outlook.MAPIFolder
        Dim myAttachments As Outlook.Attachments
        Dim Att As String

        'location to save in.  Can be root drive or mapped network drive.
        Const attPath As String = "C:\"

        ' save attachment
        Set myAttachments = item.Attachments
        Att = myAttachments.item(1).DisplayName
        myAttachments.item(1).SaveAsFile attPath & Att

        ' mark as read
       Msg.UnRead = False
    End If
End If

ProgramExit:
    Exit Sub

ErrorHandler:
    MsgBox Err.Number & " - " & Err.Description
    Resume ProgramExit
End Sub
4

1 回答 1

1

当您打开 VBA 窗口时,您将看到名为“ThisOutlookSession”的对象,这是您放置代码的位置。

此事件在收到收到的新电子邮件时自动触发:

Private Sub Application_NewMail()

Dim oInbox As MAPIFolder
Dim oItem As MailItem


Set oInbox = Application.Session.GetDefaultFolder(olFolderInbox)
Set oItem = oInbox.Items.GetLast

//MsgBox oItem.To
//Etcetera 

End Sub

关于您的编辑,我并没有真正调查它为什么不起作用,但您可以使用它,我测试过:

Dim atmt As Outlook.Attachment
Dim Att As String
Const attPath As String = "U:\"


For Each atmt In Msg.Attachments
    Att = atmt.DisplayName
    atmt.SaveAsFile attPath & Att
Next

请注意,您似乎没有保存文件,因为您无法在 WinExplorer 中使用“修改日期”来显示最新保存的附件(我刚刚注意到)。但是你可以按字母顺序查找。

于 2012-10-24T12:24:40.947 回答