在通过电子邮件发送附件之前,如何获取附件的文件名?

问题描述

如果要发送给特定用户,我正在使用VBA进行提示
我还希望它显示我要附加的文件名。

Private Sub Application_ItemSend(ByVal Item As Object,Cancel As Boolean)

    Dim xPrompt As String
    Dim xOkOrCancel,Sty As Integer
    Dim recip As Recipient
    Dim att As Attachment

    On Error Resume Next
    
    For Each att In Item.Attachments
        Debug.Print att.FileName
    Next att

    xPrompt = "Do you want to continue sending the email to the following receipients with this file?"

    For Each recip In Item.Recipients
        xPrompt = xPrompt & vbNewLine & recip & att
    Next

    Sty = vbOKCancel + vbQuestion + vbDefaultButton2
    xOkOrCancel = MsgBox(xPrompt,Sty)

    If xOkOrCancel <> vbOK Then
        Cancel = True
    End If

End Sub

解决方法

当然,您的代码无法正常工作,因为您迭代了附加列表,但没有将每次迭代的结果都放入变量中。最后,您将获得att变量以及列表的最后一个值。您的代码应如下所示:

Private Sub Application_ItemSend(ByVal Item As Object,Cancel As Boolean)

    Dim xPrompt As String
    Dim xOkOrCancel,Sty As Integer
    Dim recip As Recipient
    Dim att As Attachment
    Dim sFilesList As String
    Dim NeedToWarn As Boolean

    On Error Resume Next
    
    sFilesList = ""
    For Each att In Item.Attachments
        sFilesList = sFilesList & att.FileName & ","
    Next att
    
    sFilesList = Left(sFilesList,Len(sFilesList) - 2) 'drop last comma
    
    NeedToWarn = False

    For Each recip In Item.Recipients
        If recip.Address = "[email protected]" Then
            NeedToWarn = True
            
            xPrompt = "Do you want to continue sending the email to the following receipients with this file?"
            
            xPrompt = xPrompt & vbNewLine & vbNewLine & sFilesList
        End If
    Next
    
    If NeedToWarn Then
        Sty = vbOKCancel + vbQuestion + vbDefaultButton2
        xOkOrCancel = MsgBox(xPrompt,Sty)

        If xOkOrCancel <> vbOK Then
    
            Cancel = True
    
        End If
    End If
    

End Sub

Get attachments file names from emails vba