Vba Outlook回复然后删除邮件项目
这段代码有点粗糙,但它只是一个模型。它的基本功能是: 当从我的电子邮件中收到一封电子邮件时,主题行以“批处理”开头:Vba Outlook回复然后删除邮件项目,vba,email,outlook,outlook-2010,Vba,Email,Outlook,Outlook 2010,这段代码有点粗糙,但它只是一个模型。它的基本功能是: 当从我的电子邮件中收到一封电子邮件时,主题行以“批处理”开头: 将电子邮件复制到其他文件夹 回复电子邮件 删除原始电子邮件 目前正在做我想做的一切,除了最后删除电子邮件。在我回复了邮件后,似乎无法获得原始邮件对象来删除它。我知道事情会很简单,但我看不到 Sub Check_For_Ticket(MyMail As MailItem) Dim mysubject As String Dim strEmail As String 'On E
- 将电子邮件复制到其他文件夹
- 回复电子邮件
- 删除原始电子邮件
Sub Check_For_Ticket(MyMail As MailItem)
Dim mysubject As String
Dim strEmail As String
'On Error GoTo Ticketerror
myemail = "pb***********@hotmail.com"
mysubject = "Batch*"
strEmail = MyMail.SenderEmailAddress
strSubject = MyMail.Subject
If strEmail = myemail And strSubject Like mysubject Then
MsgBox "yes" & strSubject
Call pbMoveMessageToTestFolder(MyMail)
Call AutoReply(MyMail)
Else
MsgBox "no" & strSubject
End If
MsgBox "this email is: " & strSubject
'Ticketerror:
' MsgBox "There is an error "
' MsgBox Err.Number & " : " & Err.Description
End Sub
Sub pbMoveMessageToTestFolder(MyMail As MailItem)
' Works on one selected item
Dim myNameSpace As Outlook.NameSpace
Dim myInbox As Outlook.Folder
Dim myDestFolder As Outlook.Folder
Dim myItem As Object
Dim objcopy As Object
MsgBox "into 2nd sub"
Set myNameSpace = Application.GetNamespace("MAPI")
Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
' Add As many .Folders("SubfolderName") as needed
Set myDestFolder = myInbox.Folders("To_Process")
Set myItem = MyMail
'Application.ActiveExplorer.Selection.Item (1)
Dim copy As Object
Set copy = myItem.copy
'move copy to folder
copy.Move myDestFolder
MsgBox "should of copied"
Set myNameSpace = Nothing
Set myInbox = Nothing
Set myDestFolder = Nothing
Set myItem = Nothing
End Sub
Sub AutoReply(olItem As Outlook.MailItem)
'declaring variables
Dim olOutMail As Outlook.MailItem
Dim olOutMail2 As Outlook.MailItem
Dim strpbsubject As String
Dim strSubject As String
Dim Str As String
Str = olItem.Subject
MsgBox "3rd sub - subject: " & Str
strpbsubject = "This is an auto-reply from ***, confirming that " & strMid & " has been successfully received at " & Format(DateTime.Now, "dd-MM-yyyy hh:mm:ss")
'Creating the reply email
With olItem
Set olOutMail2 = olItem.Reply
With olOutMail2
.Body = strpbsubject 'Placing the body of the email in place
.Subject = "Delivered: " & Str 'appending "Delivered" to the subject line
.Send ' This is a changeable variable to display the reply change to .Display
' to deliver the response change to .Send
.UnRead = True 'makes the email unread after the auto response is sent
End With
Set olOutMail2 = Nothing
End With
End Sub
尝试添加
MyMail。在呼叫自动回复(MyMail)
谢谢,我会尝试一下,明天会回来更新,因为我确信当我上次尝试它时,它不起作用。然而,我在想,如果我可以将电子邮件对象移动到已删除的文件夹中,会吗?然后运行了一个宏,清除了已删除的文件夹?@Paul MyMail.Delete将该项目移动到已删除文件夹谢谢Om3r,今天下午终于可以运行了,明天我将发布代码。但这只是另一个简单的问题。如果您注意到,在代码的开始处,我正在设置来自电子邮件的。我在第一个子程序中这样做。我真的想把它分成一个自己的程序。我原以为我可以在全局变量中这样做,但这不起作用,是否必须在公共函数中删除它们(真的需要myemail和mysubject?@Paul正在从outlook规则中调用此功能?是的,我是,但如果可能的话,我尝试在via中执行所有规则,而不是将它们放在outlook规则中。。。。
If strEmail = myEmail And mysubject Like mysubject Then
MsgBox "yes" & mysubject
Call pbMoveMessageToTestFolder(MyMail)
Call AutoReply(MyMail)
MyMail.Delete
Else