发送Outlook邮件后Excel VBA提示消息

精神666阴影

我不是VBA编程专家,对某些人来说,我的问题听起来很愚蠢,但是我很感谢您的协助。

我有一个excel用户窗体,单击按钮即可生成Outlook电子邮件,并且用户手动单击邮件上的发送选项。

我的问题是系统将在Excel数据库中注册电子邮件内容(仅当用户通过Withevents类单击Outlook中的“发送选项”时),但是如果数据库不可用,则会出现一条错误消息,提示您对于用户来说,真正的问题是提示没有显示给用户(由Outlook电子邮件覆盖),因为正在处理excel代码,并且电子邮件发送过程将等待完成。

无论如何,我是否可以在Outlook顶部显示消息框或运行代码以保存到数据库,但是仅在单击“发送”选项之后?以下是我正在使用的代码,感谢您的反馈

下面的代码在用户窗体中填写并在Outlook中显示电子邮件

Public itmevt As New CMailItemEvents
Public Outapp As Outlook.Application
Public Outmail As Outlook.MailItem
public subject as string
public body as string



Private Sub SendMail_Click()
Set Outapp = New Outlook.Application
Set Outmail = Outlook.Application.CreateItem(0)
Set itmevt.itm = Outmail
body=userform.text1.text
subject=userform.text2.text
itmevt.itm.Subject = "Some Subject"
With itmevt.itm
.HTMLBody = Body
.Subject = subject
.Display
End With

这是称为(CMailItemEvents)的类的代码,用于检测发送选项

Option Explicit
Public WithEvents itm As Outlook.MailItem

Private Sub itm_Send(Cancel As Boolean)

 EmailsForm.savedetails

End Sub

单击发送选项后,将运行要保存的代码

sub savedetails()

--->Open Excel DB

If DB.ReadOnly Then
Msgbox ("Error Message Here") ----> here is the problem, the message shows on excel
--- but the outlook mail is on the front of the screen

exit sub
else

--->Save details to DB

End Sub

我试图使代码示例尽可能简短。

预先感谢您的协助

精神666阴影

我终于可以通过一种解决方法来做到这一点,我不确定这是否会帮助任何人。

我创建了另一个事件观察器,以检测实际何时关闭“电子邮件”窗口,并据此触发消息。

这是更新的类,用于检测“发送点击”和“电子邮件停用”事件:

Option Explicit
Public WithEvents itm As Outlook.MailItem
Public WithEvents appv As Inspector ----> this part is added
Public Sent as Boolean

 Private Sub itm_Send(Cancel As Boolean)
Sent=True ---> Sending E-mail Check
EmailsForm.ETo = itm.To
EmailsForm.ECC = itm.CC
EmailsForm.savedetails

End Sub

---This Part Is Added---
Private Sub appv_Deactivate()
If Sent = True then ---> Sending E-mail Check To Avoid Triggering the watcher if the E-mail is closed without sending
   if EmailsForm.Bool=true then
   msgbox ("Error Message Here")
   EmailsForm.Book=False
   Sent=False
   End If
End If
End Sub

当用户单击用户上的按钮时,将触发以下代码:

Public itmevt As New CMailItemEvents
Public Outapp As Outlook.Application
Public Outmail As Outlook.MailItem
public subject as string
public body as string



Private Sub SendMail_Click()
Set Outapp = New Outlook.Application
Set Outmail = Outlook.Application.CreateItem(0)
Set itmevt.itm = Outmail
Set itmevt.appv = Outmail.GetInspector ----> this is added to link the E-mail window to the deactivation trigger
body=userform.text1.text
subject=userform.text2.text
itmevt.itm.Subject = "Some Subject"
With itmevt.itm
.HTMLBody = Body
.Subject = subject
.Display
End With

我添加了一个布尔值,可以从通话中进行检查

public Bool as Boolean

sub savedetails()

Bool=false  ---> Boolean to be checked by the class
--->Open Excel DB

If DB.ReadOnly Then
Bool=true
exit sub
else

--->Save details to DB

End Sub

我希望以上内容清楚,可以为遇到类似问题的任何人提供帮助;谢谢大家的支持

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

(VBA Outlook)发送一揽子邮件时提示消息框

VBA:通过Mac版Excel 2011中的Outlook发送邮件

如何在excel中使用vba在outlook中发送邮件

VBA-如何在Outlook中发送电子邮件之前在Excel中存储.SentOn

使用Excel VBA发送带有附件的Outlook电子邮件

使用 Outlook VBA 将邮件类别数据发送到 Excel

使用Access VBA在批量电子邮件中提示自定义消息

Excel VBA:能够创建和发送 Outlook 电子邮件,但想要自动单击公司安全弹出窗口上的“发送”按钮

使用Excel VBA将每日收到的电子邮件提示从Outlook导出到文件

从Excel发送Outlook电子邮件,将最后一行范围文本放在正文消息中

使用Task Scheduler发送Outlook电子邮件时,Excel VBA代码引发运行时错误429

VBA 从 Excel 发送电子邮件

发送时电子邮件正文丢失(outlook vba)

应用于主题并发送邮件Outlook VBA

复制邮件并使用VBA发送到Outlook

如何通过代码发送提示消息?

通过 smtp 发送电子邮件后显示消息

从Word VBA阻止Excel提示

在VBA代码中设置olMail = Nothing设置olApp = Nothing可以做什么以通过excel从Outlook中发送电子邮件?

通过Excel VBA通过Outlook发送电子邮件-将字符串转换为货币格式或百分比

在Outlook邮件正文Excel VBA中嵌入图片

VBA excel:日期过滤器无法提取 Outlook 邮件

Excel VBA 代码生成电子邮件但使 Outlook 崩溃

如何在Outlook草稿邮件窗口中显示提示性消息?

向邮件发送消息

在Outlook中将选定电子邮件的电子邮件正文显示为Excel中的消息框?

通过VBA从Excel发送电子邮件附件

如果值为“否”,则 Excel VBA 发送邮件

Excel 2010电子邮件VBA发送整个工作簿