简体   繁体   English

使用 Excel VBA 发送 Outlook 邮件后的提示信息

[英]Prompt Message After Sending Outlook Mail using Excel VBA

I have an Excel UserForm which upon a button click generates an Outlook E-mail and the user manually clicks the send option on the mail.我有一个 Excel 用户窗体,单击按钮会生成一个 Outlook 电子邮件,用户手动单击邮件上的发送选项。

The system will register the E-mail content in an Excel database (only if the user clicks on the Send Option in Outlook through an Withevents Class).系统将在 Excel 数据库中注册电子邮件内容(仅当用户通过 Withevents 类单击 Outlook 中的发送选项时)。

If the database is not available there is an error message which should prompt the user.如果数据库不可用,则会出现一条错误消息提示用户。 The prompt is not showing to the user (covered by the Outlook E-mail) because Excel code is processing and the E-mail sending process will be waiting for it to be done.提示没有显示给用户(由 Outlook 电子邮件覆盖),因为 Excel 代码正在处理,电子邮件发送过程将等待它完成。

Is there any way I can show the message box on top of Outlook or run the code to save to the database but only AFTER the Send option is clicked?有什么方法可以在 Outlook 顶部显示消息框或运行代码以保存到数据库但仅在单击“发送”选项之后?

The code in the Userform to fill and display the E-mail in 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

this is the code for the Class called (CMailItemEvents) to detect the Send Option Click这是名为 (CMailItemEvents) 的类的代码,用于检测发送选项单击

Option Explicit
Public WithEvents itm As Outlook.MailItem
       
Private Sub itm_Send(Cancel As Boolean)

 EmailsForm.savedetails

End Sub

once the Send Option is clicked the code to save will run单击发送选项后,将运行要保存的代码

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

I tried to keep the code sample as short and simple as possible.我试图使代码示例尽可能简短。

I was finally able to do it with a workaround, I'm not sure if this is going to help anyone.我终于能够通过解决方法来做到这一点,我不确定这是否会对任何人有所帮助。

I've created another event watcher to detect when the E-mail window is actually closed, and according to that the message will be triggered.我已经创建了另一个事件观察器来检测电子邮件窗口何时实际关闭,并根据该消息将被触发。

this is the updated Class to detect the Send click & the E-mail deactivation event:这是更新的类,用于检测发送点击和电子邮件停用事件:

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

when the user click the button on the user form the following code it triggered:当用户单击用户窗体上的按钮时,它会触发以下代码:

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

I've added a Boolean to be checked from the call我添加了一个布尔值以从调用中检查

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

I hope the above is clear and can help anyone with similar issue;我希望以上内容很清楚,可以帮助任何遇到类似问题的人; thank you for your support everyone谢谢大家的支持

I've had to deal with stubborn applications myself.我不得不自己处理顽固的应用程序。 Try hiding the application then showing it before your msgbox.尝试隐藏应用程序然后在您的消息框之前显示它。

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

Probably not the most elegant of solutions - but it usually works.可能不是最优雅的解决方案 - 但它通常有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM