Create a task from a userform

A

Albert

Hi guys,

I am trying to create a task from a commandbutton in a userform.
This is what I got so far:
Sub CreateTask()

Dim oApp As Object
Set oApp = CreateObject("Outlook.Application")

Dim objTask As Object


Set objTask = oApp.CreateItem(olFolderTasks)
With objTask
.Subject = "Test"
.Body = "Test"
.DueDate = DateValue("06/26/03")
.Startdate = DateValue("06/26/03")
.Send
.Close olSave
End With
Set objTask = Nothing
End Sub

Obviously it is not working as I get a runtime error "Object does not
support the object or method" at the "duedate.

Can anyone help me with the correct code?

Thanks
Albert
 
J

JP

It should be "Set objTask = oApp.CreateItem(olTaskItem)"

"olFolderTasks" is the constant for the default Tasks folder.


HTH,
JP
 
A

Albert

Hi JP,

Thanks for the response, but I still get the same error "Object does not
support the object or method" at the "duedate".

Albert
 
J

JP

It worked on my machine, but I set a reference to the Outlook object
library. You should consider rewriting your code to take advantage of
this. For example:

Dim oApp As Outlook.Application ' not Object
Dim objTask As Outlook.TaskItem ' not Obect

Set oApp = New Outlook.Application ' early binding
Set objTask = oApp.CreateItem(olTaskItem)
With objTask
.Subject = "Test"
.Body = "Test"
.DueDate = DateValue("06/26/03")
.Startdate = DateValue("06/26/03")
.Send
.Close olSave ' I believe this is moot as the item is already
closed/sent
End With

Set objTask = Nothing
Set oApp = Nothing
End Sub

You would need to set a reference to the Microsoft Outlook Object
Library by going to Tools>References in the VB Editor and locating the
appropriate OL. For example if you were using Office 2003, then it
would be "Microsoft Outlook 11.0 Object Library".


HTH,
JP
 
A

Albert

Hi JP,

I have changed the reference in VB tools and the code:
Sub CreateTask()
Dim oApp As Outlook.Application ' not Object
Dim objTask As Outlook.taskItem ' not Obect

Set oApp = New Outlook.Application ' early binding
Set objTask = oApp.CreateItem(olTaskItem)
With objTask
.Subject = "Test"
.Body = "Test"
.DueDate = DTPickerTaskduedate.Value 'DateValue("06/26/03")
.Startdate = DTPicker1.Value 'DateValue("06/26/03")
.Send

End With

Set objTask = Nothing
Set oApp = Nothing
End Sub

I am using office 12. I now get a error "you cannot send this task because
it is not in a valid state. Use the assign method first"

Any ideas?

Thanks
A
 
J

JP

Is that Outlook 2007? I'm not familiar with that version. You might
want to repost your last message as a new topic, and be sure to
specify the version of Outlook.

Also you mentioned that you were running this from a command button on
a userform, if so shouldn't the Sub line be something like "Sub
CommandButton1_click()" ?

Sorry I couldn't be more help here.

--JP
 

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