如何检查 .Attachment.Add “filename"发送前成功 [英] How to check if .Attachment.Add "filename" is successful before send

查看:58
本文介绍了如何检查 .Attachment.Add “filename"发送前成功的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码可以创建一个邮件对象 (Outlook),附加一个文件并发送它.

I have some code that creates a Mail object (Outlook), attaches a file and sends it.

Dim mobjOutlook, mobjActiveExp, mobjNewMail As Object

'Create Outlook objects
Set mobjOutlook = CreateObject("Outlook.Application")
Set mobjActiveExp = mobjOutlook.ActiveExplorer
Set mobjNewMail = mobjOutlook.CreateItem(olMailItem)

'Setup and send email
With mobjNewMail
    .To = "someone@test.com"
    .Subject = "theSubject"
    .Body = "some text"
    .Attachments.Add "C:/The/File/Path.doc"
    '*I need to check here if the above line worked*
    .Send
End With

如何在发送前测试附件是否有效?这可能吗?出于某种原因,即使没有,它仍然会发送没有附件的电子邮件.

How can I test if the attachment works before sending? Is this possible? For some reason even if it doesn't, it still sends the email without the attachment.

我正在考虑以某种方式使用.Save"选项.

I was thinking of somehow utilizing the '.Save' option.

非常感谢任何想法或建议,谢谢.

Any thoughts or suggestions are much appreciated, thanks.

推荐答案

您可以测试电子邮件中的附件数量 > 0

You could just test the number of attachments in the email were > 0

还有

将 mobjOutlook、mobjActiveExp、mobjNewMail 作为对象
将前两个变量作为变体变暗,所以我在下面重新剪辑

Dim mobjOutlook, mobjActiveExp, mobjNewMail As Object
will dim the first two variables as variants, so I have recut this below

Sub Test()
Dim mobjOutlook As Object
Dim mobjActiveExp As Object
Dim mobjNewMail As Object

'Create Outlook objects
Set mobjOutlook = CreateObject("Outlook.Application")
Set mobjActiveExp = mobjOutlook.ActiveExplorer
Set mobjNewMail = mobjOutlook.CreateItem(olMailItem)

'Setup and send email
With mobjNewMail
    .To = "someone@test.com"
    .Subject = "theSubject"
    .Body = "some text"
    .attachments.Add "C:\temp\step1.png"
    If .attachments.Count > 0 Then
        .Send
    Else
        MsgBox "No attachment", vbCritical
    End If
End With
End Sub

这篇关于如何检查 .Attachment.Add “filename"发送前成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆