Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have the following code that worked just fine in VS2010 but is throwing an error in VS2015, is anybody able to please tell me what is wrong ?

The error is on the Dim objMessage line and is as follows :

"An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in KA_Newsletter.exe

Additional information: The operation failed. An object could not be found."


VB
Dim objOutlook As Outlook._Application
objOutlook = New Outlook.Application()
Dim objNS As Outlook._NameSpace = objOutlook.Session
Dim objTasks As Outlook.MAPIFolder = objNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderTasks)
Dim objItems As Outlook._Items = objTasks.Items
Dim objMessage As Outlook._TaskItem = objItems.Item(myLeague & " Newsletter") <<< Error

With objMessage
    .Status = 1
    .PercentComplete = 10
    .Categories = "Newsletters : In Progress"
    .Save()
End With

objTasks = Nothing
objNS = Nothing
objOutlook = Nothing
Posted
Comments
Richard MacCutchan 9-Jan-16 8:55am    
The error message seems clear enough. Try using your debugger to get more detailed information.
Gary Heath 9-Jan-16 9:35am    
It's not clear to me though, hence my question. I dabble in this stuff, I am far from an expert ...
Michael_Davies 9-Jan-16 9:12am    
Not used this before but does objItems.Item return an item if it finds an existing item based on the string (if so is it finding it) or does it create a new Item and return it (which would be odd as it is normal practice to have and Item.Add)?
Gary Heath 9-Jan-16 9:37am    
I really don't know Michael, this is code that I got from the Internet about 5 years ago and has always worked. I have upgraded to VS2015 & this happens ... as I just said to Richard, above, I'm not a professional in this, just a dabbler.
Richard MacCutchan 9-Jan-16 11:14am    
code that I got from the Internet
Your first mistake.
It is very difficult to offer much in the way of support to questions like this, when the questioner does not understand the code. All we can do is point out the obvious, which is to use your debugger to try and find out why the relevant object could not be found.

Use the debugger and ensure that objItems contains a valid reference to tasks folder.

If it doesn't the problem is that default folder for tasks cannot be found. Check using Outlook that the folder correctly exists. If necessary try recreating the account on the client side.

If it does, it seems that the item simply does not exist in tasks. Check what is the content of myLeague to know what is the name of the task the code is searching for.
 
Share this answer
 
Thank you all for your help, but I couldn't get this working so I went with an alternative method that works fine ...

VB
Dim objOutlook As Outlook._Application = New Outlook.Application
Dim objNS As Outlook._NameSpace = objOutlook.Session
Dim objTasks As Outlook.MAPIFolder = objNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderTasks)
Dim objItems As Outlook._Items = objTasks.Items
Dim objTask As Outlook.TaskItem = DirectCast(objOutlook.CreateItem(Outlook.OlItemType.olTaskItem), Outlook.TaskItem)
Dim iCount As Int16 = objItems.Count
Dim i As Int16
Dim myTaskExists As Boolean = False

For i = 1 To iCount
    If TypeOf (objItems.Item(i)) Is Outlook.TaskItem Then
        If objItems.Item(i).Subject = myLeague & " Newsletter" Then
            myTaskExists = True
        End If
    End If
Next

If myTaskExists = False Then
    objTask.Status = Outlook.OlTaskStatus.olTaskInProgress
    objTask.PercentComplete = 10
    objTask.Importance = Outlook.OlImportance.olImportanceHigh
    objTask.Categories = "Newsletters : In Progress"
    objTask.Subject = myLeague & " Newsletter"
    objTask.Save()
    ' Clean up.
    objTask = Nothing
End If
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900