为什么我在VS2015中遇到VS2010中有效的代码错误? [英] Why am I getting an error in VS2015 for code that worked in VS2010 ?

查看:262
本文介绍了为什么我在VS2015中遇到VS2010中有效的代码错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码在VS2010中运行得很好但在VS2015中抛出错误,有人能告诉我有什么问题吗?



错误在Dim objMessage行上,如下所示:



KA_Newsletter中发生了'System.Runtime.InteropServices.COMException'类型的未处理异常。 exe



附加信息:操作失败。无法找到物体。




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."


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

推荐答案

使用调试器并确保 objItems 包含对tasks文件夹的有效引用。



如果不是问题,则无法找到任务的默认文件夹。使用Outlook检查文件夹是否正确存在。如果有必要,尝试在客户端重新创建帐户。



如果确实如此,似乎项目根本不存在于任务中。检查 myLeague 的内容是什么,以了解代码搜索的任务的名称。
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.


谢谢大家你的帮助,但我不能让这个工作,所以我选择了另一种工作正常的方法...



Thank you all for your help, but I couldn't get this working so I went with an alternative method that works fine ...

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


这篇关于为什么我在VS2015中遇到VS2010中有效的代码错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆