【问题标题】:Prompt Message After Sending Outlook Mail using Excel VBA使用 Excel VBA 发送 Outlook 邮件后提示消息
【发布时间】:2021-06-05 07:19:36
【问题描述】:

我有一个 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

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

【问题讨论】:

  • 尝试AppActivate Application.Caption 来推动 Excel。
  • 我试过了,但没用 :-(
  • 它什么也没做?
  • 它什么也没做,
  • 尝试另一种方法,例如从这里 - stackoverflow.com/questions/34481674/…

标签: excel vba outlook


【解决方案1】:

我终于能够通过解决方法做到这一点,我不确定这是否会对任何人有所帮助。

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

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

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

我希望以上内容很清楚,可以帮助任何有类似问题的人;谢谢大家的支持

【讨论】:

    【解决方案2】:

    我不得不自己处理顽固的应用程序。尝试隐藏应用程序,然后将其显示在您的 msgbox 之前。

    If DB.ReadOnly Then
        Application.Visible = False
        Application.Visible = True
        MsgBox "Error Message Here"
    End If
    

    可能不是最优雅的解决方案 - 但它通常有效。

    【讨论】:

    • 我刚刚对此进行了测试,但也没有用,我认为问题在于代码正在运行 (Private Sub itm_Send(Cancel As Boolean)) 仍在运行的内容。
    • 退出Send Sub后有什么方法可以运行代码吗?我四处寻找,但找不到任何东西。
    猜你喜欢
    • 2013-05-27
    • 1970-01-01
    • 2018-03-07
    • 1970-01-01
    • 2022-08-14
    • 2021-06-28
    • 1970-01-01
    • 1970-01-01
    • 2019-03-22
    相关资源
    最近更新 更多