Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

outlook tasks in VBA

Status
Not open for further replies.

msstrang

Programmer
Sep 19, 2005
62
US
happy holidays everyone, i hope the weather isn't too cold where you are.

i'm modifying a code i wrote early this year that creates an email based on a spreadsheet. i want the code to take the body of the open email and make that the body of an outlook task, take the subject of the open email, and make that the subject of the task, and then to save the task to a folder on my company's network drive. so far i have gotten pretty far, but with two small problems.

1. (and this is the problem i'm most concerned with) when i try to use the saveas command on my task i get the outlook security warning. i'm already using redemption to get the body of my open email, but i'm not sure how to use redemption (or if you can) to set the save path for my outlook task to avoid the security warning.

2. the method i'm using to get the body of my open email that is being written by the program saves a copy of the email as a draft, however, when the program finishes i want the email to be open (display) but i don't want the copy of the email in the drafts box. when i try to delete the email from the drafts box programmicaly it closes the open instance of the email as well.

below is the code:

sub createtasks()
Dim olTask As Outlook.TaskItem
Set olTask = OlApp.CreateItem(olTaskItem)
olTask.Subject = NewMail.Subject
NewMail.Save
redemptionTaskBody taskbody
olTask.Body = taskbody
olTask.DueDate = DateAdd("d", 21, Now)
olTask.Display
olTask.SaveAs ("I:\AutocadData\Temp\" + NewMail.Subject)'this line creates security warning
end sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub redemptionTaskBody(taskbody)
Dim OlApp As Outlook.Application
Dim sItem
Dim oitem As MailItem

Set OlApp = Outlook.Application
Set sItem = CreateObject("Redemption.SafeMailItem")
Set oitem = OlApp.Session.GetDefaultFolder(olFolderDrafts).Items(1) 'gets the first item in the drafts folder
sItem.Item = oitem
PrTaskBody = &H1000001E'gets the body of the object so i can set it to my task body
taskbody = sItem.Fields(PrTaskBody)

oitem.Delete'this line deletes the item in the drafts folder, but also closes the open instance of the email which i need to be open for the rest of my code
End Sub

any help would be greatly appreciated/
 
in regards toi the second problem, if there is a copy how about duplkicating the mailitem , for example


Dim olitem2 as mailitem
{
set olitem2 = olitem
{
olitem.delete



Chance,

Filmmaker, gentlemen and read my blog at
 
i took your advise and slightly tweaked it so it would work. Creating a copy of the already existing instances of the email still resulted in the msg being removed when i removed the email from the draft box, however i just created two new emails and added the same text to both emails, and then deleted the first one and kept the second to remove my issue.

also, i figured out how to get outlook tasks to work using redemption, below is the code i used.

Dim oltask As TaskItem
Dim mySafeTask As Object

Set oltask = OlApp.CreateItem(olTaskItem)
Set mySafeTask = CreateObject("Redemption.SafeTaskItem")
mySafeTask.Item = oltask

mySafeTask.Subject = newMail.Subject
newMail.Save
redemptionTaskBody taskbody
mySafeTask.Body = taskbody
mySafeTask.DueDate = DateAdd("d", 21, Now)
mySafeTask.Display
mySafeTask.SaveAs ("I:\AutocadData\Temp\" + NewMail2.Subject)
mySafeTask.Close olDiscard

this code works with no errors. thanks for help on my second issue, it's muchly appreciated.
 
i take that back.
when i try to drag and drop mySafeTask from the I:\AutocadData\Temp\ folder i saved it in programmically, it is empty.
when i save oltask (the non redemption version of this file) to that folder i get the security warning but the task opens properly
also, when i look at mysafetask.display, everything seems in order.

what am i doing wrong? it seems like something very simple... should i specify the type of file in my save as call up.
 
sub outlookTaskCreate ()
Dim OlApp As Outlook.Application
Dim newMail As Outlook.MailItem
Dim NewMail2 As Outlook.MailItem
Set OlApp = Outlook.Application
Set newMail = OlApp.CreateItem(olMailItem)
Set NewMail2 = OlApp.CreateItem(olMailItem)

newMail.Subject = "Subject"
NewMail2.Subject = "Subject"
NewMail.Body="Body"
NewMail2.Body="Body"
NewMail.Display
NewMail2.Display

Dim oltask As TaskItem
Dim mySafeTask As Object

Set oltask = OlApp.CreateItem(olTaskItem)
Set mySafeTask = CreateObject("Redemption.SafeTaskItem")
mySafeTask.Item = oltask

mySafeTask.Subject = newMail.Subject
newMail.Save
redemptionTaskBody taskbody
mySafeTask.Body = taskbody
mySafeTask.DueDate = DateAdd("d", 21, Now)
mySafeTask.Display
mySafeTask.SaveAs ("I:\AutocadData\Temp\" + NewMail2.Subject)
mySafeTask.Close olDiscard
end sub

Sub redemptionTaskBody(taskbody)
Dim OlApp As Outlook.Application
Dim sItem
Dim oitem As MailItem

Set OlApp = Outlook.Application
Set sItem = CreateObject("Redemption.SafeMailItem")
Set oitem = OlApp.Session.GetDefaultFolder(olFolderDrafts).Items(1) 'get first e-mail from the Inbox, can be any other item
sItem.Item = oitem
PrTaskBody = &H1000001E
taskbody = sItem.Fields(PrTaskBody)

oitem.Delete
End Sub

the saveas command works properly if i specify oltask.saveas ("I:\AutocadData\Temp\" + NewMail2.Subject); however, i get the security warning.

the saveas command appears to work if i specify mySafeTask.SaveAs ("I:\AutocadData\Temp\" + NewMail2.Subject); i get no security warning, but when i attempt to drag the file from the "I:\AutocadData\Temp\"
folder the file then automatically pops open with none of my data in it and it does not appear in the task log. very strange....
 
thanks for the help, but we've already solved the 2nd problem. i'm not completely sure your suggestion would work, however, because once the email would be deleted it wouldn't have an entryID property. no matter, again, thanks for the effort.

if anyone has anything to help me on the first issue i would be very grateful.

 
redemption is a library that works like CDO to avoid getting security warning in outlook when accessing certain data programmically.

could you give an example of the doevents or sendkey that you think might work without prompting a security message in outlook.
 
again, i want to thank everyone for trying to help...

i finally figured it out.

before you can save a redemption item, you have to save the outlook application equivalent of it as well, in this case the code ended up looking like this:

Dim oltask As TaskItem
Dim mySafeTask

Set oltask = OlApp.CreateItem(olTaskItem)
Set mySafeTask = CreateObject("Redemption.SafeTaskItem")


oltask.Subject = newMail.Subject
newMail.Save
redemptionTaskBody taskbody
oltask.Body = taskbody
oltask.DueDate = DateAdd("d", 21, Now)
oltask.Display
oltask.Save
mySafeTask.Item = oltask
mySafeTask.SaveAs ("I:\AutocadData\Temp\" + NewMail2.Subject)
mySafeTask.Close olDiscard
oltask.Delete

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top