Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 在通过电子邮件发送附件之前,如何获取附件的文件名?_Vba_Outlook - Fatal编程技术网

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

Vba 在通过电子邮件发送附件之前,如何获取附件的文件名?,vba,outlook,Vba,Outlook,如果要发送给特定用户,我将使用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

如果要发送给特定用户,我将使用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
私有子应用程序\u ItemSend(ByVal项作为对象,取消作为布尔值)
Dim xPrompt作为字符串
Dim XOKOR取消,Sty为整数
作为收件人的Dim recip
作为附件的Dim att
出错时继续下一步
附件项中的每个附件
Debug.Print att.FileName
下一个att
xPrompt=“是否继续使用此文件向以下收件人发送电子邮件?”
对于项目中的每个recip。收件人
xPrompt=xPrompt&vbNewLine&recip&att
下一个
Sty=vbOKCancel+vbQuestion+vbDefaultButton2
xOkOrCancel=MsgBox(xPrompt,Sty)
如果要取消vbOK,则
取消=真
如果结束
端接头

当然,您的代码工作不正确,因为您迭代了附加列表,但并没有将每次迭代的结果放入变量。最后是带有列表最后一个值的
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 = "somebody@domain.com" 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
私有子应用程序\u ItemSend(ByVal项作为对象,取消作为布尔值)
Dim xPrompt作为字符串
Dim XOKOR取消,Sty为整数
作为收件人的Dim recip
作为附件的Dim att
Dim sFilesList作为字符串
Dim NeedToWarn为布尔型
出错时继续下一步
sFilesList=“”
附件项中的每个附件
sFilesList=sFilesList&att.FileName&“
下一个att
sFilesList=Left(sFilesList,Len(sFilesList)-2)“删除最后一个逗号
NeedToWarn=False
对于项目中的每个recip。收件人
如果recip.Address=”somebody@domain.com”“那么
NeedToWarn=True
xPrompt=“是否继续使用此文件向以下收件人发送电子邮件?”
xPrompt=xPrompt&vbNewLine&vbNewLine&sFilesList
如果结束
下一个
如果需要,那么
Sty=vbOKCancel+vbQuestion+vbDefaultButton2
xOkOrCancel=MsgBox(xPrompt,Sty)
如果要取消vbOK,则
取消=真
如果结束
如果结束
端接头

当然,您的代码工作不正确,因为您迭代了附加列表,但并没有将每次迭代的结果放入变量。最后是带有列表最后一个值的
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 = "somebody@domain.com" 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
私有子应用程序\u ItemSend(ByVal项作为对象,取消作为布尔值)
Dim xPrompt作为字符串
Dim XOKOR取消,Sty为整数
作为收件人的Dim recip
作为附件的Dim att
Dim sFilesList作为字符串
Dim NeedToWarn为布尔型
出错时继续下一步
sFilesList=“”
附件项中的每个附件
sFilesList=sFilesList&att.FileName&“
下一个att
sFilesList=Left(sFilesList,Len(sFilesList)-2)“删除最后一个逗号
NeedToWarn=False
对于项目中的每个recip。收件人
如果recip.Address=”somebody@domain.com”“那么
NeedToWarn=True
xPrompt=“是否继续使用此文件向以下收件人发送电子邮件?”
xPrompt=xPrompt&vbNewLine&vbNewLine&sFilesList
如果结束
下一个
如果需要,那么
Sty=vbOKCancel+vbQuestion+vbDefaultButton2
xOkOrCancel=MsgBox(xPrompt,Sty)
如果要取消vbOK,则
取消=真
如果结束
如果结束
端接头

尝试了这个方法,但在附加多个附件时,它不读取。你说的“它不读取”是什么意思?你能展示你的全部代码吗?我刚刚用你的建议编辑了原始帖子中的代码me@GHOSTR3V3RB我更改了我的答案,请检查它是否打印了此内容,但在附加多个附件时,它不会读取。你说的“它不会读取”是什么意思?你能展示你的全部代码吗?我刚刚用你的建议编辑了原始帖子中的代码me@GHOSTR3V3RB我更改了答案,请检查