V
vbadude_sl
Hello
I have written av vba code that creates tasks from mails and another code
that finds via mail subject and deletes the task - but the second code that
deletes doesnt work - it will not find the task with the items.find command
Here is my code:
Sub Done ()
Dim oExplorer As Outlook.Explorer
Dim omail As Outlook.MailItem
Dim oOldMail As Outlook.MailItem
Dim StrSubject
Dim mailsubject
Set oExplorer = Application.ActiveExplorer
If oExplorer.Selection.Item(1).Class = olMail Then
Set oOldMail = oExplorer.Selection.Item(1)
Set omail = oOldMail.Reply
omail.SentOnBehalfOfName = "(e-mail address removed)"
omail.To = oOldMail.SenderName
omail.Subject = oOldMail.Subject
omail.Recipients.Item(1).Resolve
If omail.Recipients.Item(1).Resolved Then
omail.Body = vbCrLf & "Hello test" & omail.Body
omail.Display
Else
MsgBox "Error" & omail.Recipients.Item(1).Name
End If
Else
MsgBox "This is not an email"
Exit Sub
End If
mailsubject = "[Subject] =" & omail.Subject
Call DeleteProjectTask
End Sub
Sub DeleteProjectTask()
On Error Resume Next
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Dim olFolders As Outlook.Folders
Dim bcmProjectTasksFolder As Outlook.MAPIFolder
Dim existProjectTask As Outlook.TaskItem
Set olApp = CreateObject("Outlook.Application")
Set objNS = olApp.GetNamespace("MAPI")
Set olFolders = objNS.Session.Folders
Set bcmProjectTasksFolder = objNS.GetDefaultFolder(olFolderTasks)
Set existProjectTask = bcmProjectTasksFolder.Items.Find(mailsubject)
If Not TypeName(existProjectTask) = "Nothing" Then
existProjectTask.Delete
Else
MsgBox ("Cannot find task")
End If
End Sub
I have written av vba code that creates tasks from mails and another code
that finds via mail subject and deletes the task - but the second code that
deletes doesnt work - it will not find the task with the items.find command
Here is my code:
Sub Done ()
Dim oExplorer As Outlook.Explorer
Dim omail As Outlook.MailItem
Dim oOldMail As Outlook.MailItem
Dim StrSubject
Dim mailsubject
Set oExplorer = Application.ActiveExplorer
If oExplorer.Selection.Item(1).Class = olMail Then
Set oOldMail = oExplorer.Selection.Item(1)
Set omail = oOldMail.Reply
omail.SentOnBehalfOfName = "(e-mail address removed)"
omail.To = oOldMail.SenderName
omail.Subject = oOldMail.Subject
omail.Recipients.Item(1).Resolve
If omail.Recipients.Item(1).Resolved Then
omail.Body = vbCrLf & "Hello test" & omail.Body
omail.Display
Else
MsgBox "Error" & omail.Recipients.Item(1).Name
End If
Else
MsgBox "This is not an email"
Exit Sub
End If
mailsubject = "[Subject] =" & omail.Subject
Call DeleteProjectTask
End Sub
Sub DeleteProjectTask()
On Error Resume Next
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Dim olFolders As Outlook.Folders
Dim bcmProjectTasksFolder As Outlook.MAPIFolder
Dim existProjectTask As Outlook.TaskItem
Set olApp = CreateObject("Outlook.Application")
Set objNS = olApp.GetNamespace("MAPI")
Set olFolders = objNS.Session.Folders
Set bcmProjectTasksFolder = objNS.GetDefaultFolder(olFolderTasks)
Set existProjectTask = bcmProjectTasksFolder.Items.Find(mailsubject)
If Not TypeName(existProjectTask) = "Nothing" Then
existProjectTask.Delete
Else
MsgBox ("Cannot find task")
End If
End Sub