Cannot Save Custom Form

D

Dave Hogg

I am trying to create a task from a custom form. My code fails on the save
method, generating the following error message.

An unhandled exception of type 'System.Runtime.InteropServices.COMException'
occurred in microsoft.visualbasic.dll

Additional information: The operation failed.

My code is as follows.

datDueDate = Now
datDueDate = datDueDate.AddDays(7)

glbOutlookNewItem = glbOutlookDir.items.add("IPM.Task.<CS Request>")

glbOutlookNewItem.Subject = "Quote Acceptance Test"
glbOutlookNewItem.DueDate = datDueDate
glbOutlookUP = glbOutlookNewItem.UserProperties
glbOutlookUP("Start Date") = datDueDate
glbOutlookUP("Due Date") = datDueDate
glbOutlookNewItem.save()

I have tested that I can create and save a task in the folder manually.

Any suggestions would be great. Thanks
 
D

Dave Kane [MVP - Outlook]

glbOutlookUP("Start Date") is an object which your code is trying to set
equal to a Date. I can't tell how you've dimensioned your objects that
allowed you do that without getting a compiler error, but strong typing is
actually your friend. Make sure you have Option Strict and Option Explicit
turned on if you're working with COM Interop, to reduce your personal
hair-pulling quotient.

Try
glbOutlookUP("Start Date").Value = datDueDate
glbOutlookUP("Due Date").Value = datDueDate
 
D

Dave Hogg via OfficeKB.com

Dave,

Many thanks for the reply, and I do take your point about the hair pulling.
I have tried as you suggest, but it still fails. I have moved the 'Save' to
line 3 and it still fails.

There seem to be a lot of people who have posted a similar problem out there
glbOutlookUP("Start Date") is an object which your code is trying to set
equal to a Date. I can't tell how you've dimensioned your objects that
allowed you do that without getting a compiler error, but strong typing is
actually your friend. Make sure you have Option Strict and Option Explicit
turned on if you're working with COM Interop, to reduce your personal
hair-pulling quotient.

Try
glbOutlookUP("Start Date").Value = datDueDate
glbOutlookUP("Due Date").Value = datDueDate
I am trying to create a task from a custom form. My code fails on the save
method, generating the following error message.
[quoted text clipped - 23 lines]
Any suggestions would be great. Thanks
 
D

Dave Kane [MVP - Outlook]

Actually, now that I look at your code more closely I think you've got an
earlier type conversion issue. I assume that glbOutlookDir is dimensioned as
a MAPIFolder object, and glbOutlookNewItem is presumably dimmed as a
TaskItem. The Outlook Items collection is not strongly typed - when you call
its Add method in VB.NET you get back an Object. You need to explicitly cast
that Object to make it a TaskItem or .NET will raise an exception.

If the Imports statement for your Outlook reference looks like this
Imports Outlook = Microsoft.Office.Interop.Outlook
the line where you instantiate your custom task item should be
glbOutlookNewItem = CType(glbOutlookDir.items.add("IPM.Task.<CS
Request>"),Outlook.TaskItem)

At that point you should verify the message class of your new item.

I hadn't noticed before that you were getting an "unhandled" exception - if
you wrap your code in try...catch blocks then you will be able to catch
exceptions so you can log the full exception details. That will help you to
determine where things start jumping the rails.

Dave Hogg via OfficeKB.com said:
Dave,

Many thanks for the reply, and I do take your point about the hair
pulling.
I have tried as you suggest, but it still fails. I have moved the 'Save'
to
line 3 and it still fails.

There seem to be a lot of people who have posted a similar problem out
there
glbOutlookUP("Start Date") is an object which your code is trying to set
equal to a Date. I can't tell how you've dimensioned your objects that
allowed you do that without getting a compiler error, but strong typing is
actually your friend. Make sure you have Option Strict and Option Explicit
turned on if you're working with COM Interop, to reduce your personal
hair-pulling quotient.

Try
glbOutlookUP("Start Date").Value = datDueDate
glbOutlookUP("Due Date").Value = datDueDate
I am trying to create a task from a custom form. My code fails on the
save
method, generating the following error message.
[quoted text clipped - 23 lines]
Any suggestions would be great. Thanks
 

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