Creating an Outlook Task Item

J

John Google

Hi,

Excel and Outlook XP version.

I copied the code from the samples.xls workbook that is in the Excel
\Samples directory to create an Outlook task item from excel. I place
a button on a sheet which calls the following macro when clicked:


Sub Button1_Click()
'For this example click References on the Tools Menu, and select the
'Microsoft Outlook 10.0 object libraries.

Dim ol As Object, myItem As Object
'Create a Microsoft Outlook session
Set ol = CreateObject("outlook.application")
'Create a task
Set myItem = ol.CreateItem(olTaskItem)
'Add information to the new task
With myItem
.Subject = "New VBA task XX"
.Body = "This task was created via Automation from Microsoft
Excel XX"
.NoAging = True
.Close (olSave)
End With
'Remove object from memory
Set ol = Nothing

End Sub

When I click the button while Outlook is open it creates an email
message in the Drafts folder. When I click the button when Outlook is
not running, the cursor changes to an hour glass for a couple of
seconds (as it created the Outlook object) however, in this case, when
I start Outlook nothing is there initially but then, after 10-20
seconds an email is placed in my Inbox with the details above. So,
Outlook is creating emails rather than a task!

I've searched the group and found the following post which is slightly
different.


Sub CreateTask()


Dim olApp As Outlook.Application
Dim olTsk As TaskItem


Set olApp = New Outlook.Application
Set olTsk = olApp.CreateItem(olTaskItem)


With olTsk
.Subject = Range("A1")
.Status = olTaskInProgress
.Importance = olImportanceHigh
.DueDate = Range("A2")
.TotalWork = 40
.ActualWork = 20
.Save
End With


Set olTsk = Nothing
Set olApp = Nothing


End Sub


However, this will not compile / run with the error 'User defined type
not defined' for the Outlook.Application type..

In Tools -> References 'Microsoft Office 10.0 Object Library' is
selected.


Does anyone know what my problem is or know of a way to do this
successfully?


Thanks for any help.


John.
 
J

John Google

I've solved it myself. I found another post are realised that I had to
add a reference for the 'Microsoft Outlook 10.0 Object Librarry' as
well as 'Microsoft Office 10.0 Object Library'.

I should have read the comments clearly!!!!
 
J

John Google

I've hit another problem. I use the code:

Sub CreateTask()

Dim olApp As Outlook.Application
Dim olTsk As TaskItem

Set olApp = New Outlook.Application
Set olTsk = olApp.CreateItem(olTaskItem)

With olTsk
.Subject = Range("A1")
.Status = olTaskInProgress
.Importance = olImportanceHigh
.DueDate = Range("A2")
.TotalWork = 40
.ActualWork = 20
.Save
End With

Set olTsk = Nothing
Set olApp = Nothing

End Sub

In A2 is have a date 14/04/2008 (UK date format DD/MM/YYYY). When I
look at it in Outlook Tasks it shows the date as 30/12/1899. This is
the same date in Outlook for various dates in the 2007 - 2008 range.

I'm assuming Outlook and Excel use different date format. Is there a
way to convert between the two? What are the rules?

Thanks!
 
J

John Google

Solved!

The cell A2 was empty so it returned 0 which is one day before
01/01/1900.
 

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