Reputation: 229
I'm very new in vb.net and outlook add-in, working on a project to build an outlook add-in (VSTO using VisualStudio 2017) where I'me getting this error for which im not getting any clue even after hours of googling, may be im not in the right direction. Thats why Im here to ask for your assistance. Thanks
Here is the error I've got, and I've pointed the line in the code where the error appears.
Error: System.Runtime.InteropServices.COMExceptions: The attempted operation failed. An object could not be found.
Update:
I was testing a little bit and found the if I don't rename the root folder problematically in this line dFolder.Name = "X" + dName
, dName = dFolder.Name
rather rename name manually within outlook add data file section then the full function works fine without any error.
Note: the purpose of this code is to copy emails from one folder to another where both root folder name is same because the destination folder is just a mirror copy of source folder And for this emails were not copying if the both folder name is same, That's why I had to rename the destination folder when i'm adding to Store Session so that system can identify the source and destination folder as two different location. I don't know why its not getting the rename value in dFolders = oNspace.Folders.Item(dName).Folders
. please help.
.
Public Shared Sub SetSrcAndDst(Src As String, Dst As String)
Dim oApp As Outlook.Application
Dim oNspace As Outlook.NameSpace
Dim sFolders As Outlook.Folders
Dim sFolder As Outlook.Folder
oApp = Globals.ThisAddIn.Application
oNspace = oApp.GetNamespace("MAPI")
sFolders = oNspace.Folders.Item(Src).Folders
Dim oStores As Outlook.Stores
Dim oStore As Outlook.Store
Dim dPath As String
Dim dName As String
Dim dFolders As Outlook.MAPIFolder
Dim dFolder As Outlook.MAPIFolder
oStores = oApp.Session.Stores
oNspace.AddStore(Dst)
dName = ""
For Each oStore In oStores
dFolder = oStore.GetRootFolder
dPath = oStore.FilePath
If dPath = Dst Then
dName = dFolder.FolderPath
dName = dName.TrimStart({"\"c})
dFolder.Name = "X" + dName
dName = dFolder.Name
End If
Next
'''
''' Getting error in this below line (dFolders = oNspace.Folders.Item(dName).Folders)
''' System.Runtime.InteropServices.COMExceptions:
''' The attempted operation failed. An object could not be found.
'''
dFolders = oNspace.Folders.Item(dName).Folders
For Each sFolder In sFolders
For Each dFolder In dFolders
If sFolder.Name = dFolder.Name Then
Call CopyMail(sFolder, dFolder)
End If
Next
Next
End Sub
($exception).StackTrace
at Microsoft.Office.Interop.Outlook.FoldersClass.get_Item(Object Index)
at AutoBackup.ThisAddIn.SetSrcAndDst(String Src, String Dst) in E:\AutoBackup\ThisAddIn.vb:line 56
at AutoBackup.SettingsForm.PstConfirmBtn_Click(Object sender, EventArgs e) in E:\AutoBackup\SettingsForm.vb:line 37
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
Upvotes: 0
Views: 1710
Reputation: 66341
The error is very unambiguous - the folder with the given name does not exist.
As a test, loop through all root folders to make sure the folder with the given name is really there:
for each vFolder in oNspace.Folders
MsgBox fFolder.Name
next
Upvotes: 1