Vbscript 发送电子邮件后退出Outlook对象
以下是我目前掌握的代码:Vbscript 发送电子邮件后退出Outlook对象,vbscript,outlook.application,Vbscript,Outlook.application,以下是我目前掌握的代码: Option Explicit Call OpenOutlook() Function OpenOutlook() Dim ObjShell Set ObjShell = CreateObject("WScript.Shell") ObjShell.Run("Outlook.exe") Call SendEmail() 'I tried closing from here but this didn't work eit
Option Explicit
Call OpenOutlook()
Function OpenOutlook()
Dim ObjShell
Set ObjShell = CreateObject("WScript.Shell")
ObjShell.Run("Outlook.exe")
Call SendEmail()
'I tried closing from here but this didn't work either
'ObjShell.Quit
End Function
Function SendEmail()
'Declaring variables used through out this function
Dim ObjOutlook
Dim objMail
Set ObjOutlook = CreateObject("Outlook.Application")
'CreateItem(0) opens a New Email window...MailItem
set objMail = ObjOutlook.CreateItem(0)
objMail.Display
'MailItem Options
objMail.to = "test@mail.com.com"
'objMail.cc = "test2@mail.com"
objMail.Subject = "Did it work!?"
objMail.Body = "If you got this email, my VBs test worked!"
'objMail.Attachments.Add("C:\Attachment\abc.jpg")
objMail.Send
'This didn't work either
'If objMail.Sent = True Then
'ObjOutlook.Quit
'End If
'Quit closes Outlook like I want but it doesn't wait for the email to send
'ObjOutlook.Quit
End Function
我正在尝试使用VBScript自动化的内容:
Set ObjOutlook = CreateObject("Outlook.Application")
'CreateItem(0) opens a New Email window...MailItem
set objMail = ObjOutlook.CreateItem(0)
objMail.Display
我所做的(甚至不确定这是否是正确的方法):
为什么我不能在调用sendmail()
函数后退出?使用.Quit
会给我一个错误
MailItem
具有一个属性,用于指示消息的发送时间。试试这个:
...
objMail.Send
Do Until objMail.Sent
WScript.Sleep 500
Loop
' Safe to close...
ObjOutlook.Quit
试试这个:
...
objMail.Send
Do Until objMail.Sent
WScript.Sleep 500
Loop
' Safe to close...
ObjOutlook.Quit
Option Explicit
Sub SendMail()
Dim outobj, mailobj
Set outobj = CreateObject("Outlook.Application")
Set mailobj = outobj.CreateItem(0)
With mailobj
.To = "user@test.com"
.Subject = "Testmail"
.Body = "If you got this email, my VBs test worked!"
.Send
End With
'Clear the memory
Set outobj = Nothing
Set mailobj = Nothing
End Sub
SendMail()
Msgbox("Done")
首先,如果是用户打开Outlook会怎么样?我不认为任何用户会欣赏你关闭Outlook的代码。在关闭Outlook之前,您可能需要检查
应用程序.Explorers.Count
和应用程序.Inspectors.Count
是否都为零。如果没有打开的探索者或检查者,最新版本的Outlook将自动存在,即使您持有对Outlook对象的引用—这样做是为了保护泄露引用的年龄歧视行为不端的应用程序。若要防止Outlook关闭,请hodl对资源管理器对象t的引用(即使未显示该对象)-调用Namespace.getDefaultFolder(olFolderInbox),然后保留对MAPIFolder.GetExplorer返回的对象的引用
其次,在调用Send之后,使用Namespace、SyncObjects集合检索第一个SyncObject。连接SyncObject.SyncEnd事件并调用SyncObject.Start。当SyncEnd事件触发时,您将完成所有操作。但是我不认为你可以用VB脚本处理事件 您需要使用Outlook吗?大多数VBScriptor使用CDO库发送电子邮件。看,这是行不通的。邮件在发送并移动到“已发送邮件”文件夹后即被发送。它将与您发送的消息不同。更重要的是,在调用Send后以任何方式触摸消息都会中止提交过程。@DmitryStreblechenko真的吗?在发送过程中无法检查
邮件项目的属性?看起来很奇怪。我猜这排除了我的下一个想法。。。正在检查MailItem
的父文件夹是否为“已发送”文件夹。是的,在调用Send之后,MialItem对象上唯一允许的操作是释放它-消息现在属于假脱机程序。谢谢大家@Bk,我试过你的代码,效果很好!你能告诉我你的最后一段代码在做什么吗?@Edrei当然。行“SendMail()”调用子例程“Sub SendMail()”(第二行)。在“Sub Sendmail()”和“End Sub”(从上到下)之间处理代码后,它会再次返回到第二行(即最后一行)“Sendmail()”。下一行创建一个带有文本“Done”的Messagebox。