Delete tasks from mail subject

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
 
K

Ken Slovak - [MVP - Outlook]

mailsubject is local to the Done() procedure and must be passed to the
DeleteProjectTask() procedure, otherwise it will be a null string. A filter
must have not only what you are looking for but the property to look in. The
filter would have to look something like:

bcmProjectTasksFolder.Items.Find("[Subject] = '" & mailsubject & "'")

Those are single quotes in between the double quotes.

If all this is running in Outlook VBA do not use CreateObject() in
DeleteProjectTask(), just use the Application object you correctly used in
Done().
 
V

vbadude_sl

Thanks for your answer - but how can i pass the mailsubject to the delete
procedure? I have tryed the call done(mailsubject) but this doesnt work


Ken Slovak - said:
mailsubject is local to the Done() procedure and must be passed to the
DeleteProjectTask() procedure, otherwise it will be a null string. A filter
must have not only what you are looking for but the property to look in. The
filter would have to look something like:

bcmProjectTasksFolder.Items.Find("[Subject] = '" & mailsubject & "'")

Those are single quotes in between the double quotes.

If all this is running in Outlook VBA do not use CreateObject() in
DeleteProjectTask(), just use the Application object you correctly used in
Done().





vbadude_sl said:
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
 
K

Ken Slovak - [MVP - Outlook]

Why wouldn't it work? All it would be since mailsubject is a string value
would be something like this:

Call Done(mailsubject)

Sub Done(mailsubject As String)
 
V

vbadude_sl

It works now - thanx for great help



Ken Slovak - said:
Why wouldn't it work? All it would be since mailsubject is a string value
would be something like this:

Call Done(mailsubject)

Sub Done(mailsubject As String)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top