Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VS2010 VB.NET Outlook
I have written this small program in VS2010 to run on Outlook 2007.
 
It works for a standard read through of the Inbox, but I cannot get it to correctly point to other Folders, I am getting a "COMException was unhandled by user code" error that says "The operation failed. An object could not be found." ...
 
My Outlook structure has "Outlook (Gary)" at the top & INBOX & Kickabout are both first level Folders with Location Properties of "\\Outlook (Gary)", whereas the Attachments folder is within Kickabout and it has a Location Property of "\\Outlook (Gary)\Kickabout".
 
So what can't be found ?!?
 
Imports Microsoft.Office.Interop
 
Public Class ThisAddIn
    Private Sub ThisAddIn_Startup() Handles Me.Startup
    End Sub
    Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown
    End Sub
    Private Sub Application_Startup() Handles Application.Startup
 
        Dim MyApp As Outlook.Application = New Outlook.Application
        Dim MyNS As Outlook.NameSpace = MyApp.GetNamespace("MAPI")
        Dim MyInbox As Outlook.MAPIFolder = MyNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
        Dim MyEmails As Integer = MyInbox.Items.Count
        Dim MyEMail As Outlook.MailItem
        Dim MyCount As Integer
        Dim MySubFolder As Outlook.MAPIFolder = MyNS.Folders("Kickabout") <<< Error occurs here
 
        For MyCount = MyEmails To 1 Step -1
            MyEMail = MyInbox.Items(MyCount)
            If MyEMail.SenderEmailAddress = "MrX@abc.com" Then
                If MyEMail.Attachments.Count > 0 Then
                    MySubFolder = MyNS.Folders("Kickabout\Attachments")
                End If
                MyEMail.Move(MySubFolder)
            End If
        Next
    End Sub
 
End Class
Posted 4-Oct-12 10:48am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

OK, I have solved this myself ... if anybody is interested in the future, you have to be quite explicit in setting up the path & need a Function to do so, here is the code ...
 
Imports Microsoft.Office.Interop
 
Public Class ThisAddIn
 
    Dim strFolderPath As String
 
    Private Sub ThisAddIn_Startup() Handles Me.Startup
    End Sub
    Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown
    End Sub
    Private Sub Application_Startup() Handles Application.Startup
 
        Dim MyApp As Outlook.Application = New Outlook.Application
        Dim MyNS As Outlook.NameSpace = MyApp.GetNamespace("MAPI")
        Dim MyInbox As Outlook.MAPIFolder = MyNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
        Dim MyEmails As Integer = MyInbox.Items.Count
        Dim MyEMail As Outlook.MailItem
        Dim MyCount As Integer
        Dim MySubFolder As Outlook.Folder = GetMyFolder("Outlook (Gary)\Kickabout")
        Stop
        For MyCount = MyEmails To 1 Step -1
            MyEMail = MyInbox.Items(MyCount)
            If MyEMail.SenderEmailAddress = "MrX@abc.com" Then
                If MyEMail.Attachments.Count > 0 Then
                    MySubFolder = GetMyFolder("Outlook (Gary)\Kickabout\Attachments")
                End If
                MyEMail.Move(MySubFolder)
            End If
        Next
    End Sub
 
    Function GetMyFolder(FolderPath)
        ' folder path needs to be something like 
        '   "Public Folders\All Public Folders\Company\Sales"
        Dim aFolders
        Dim fldr
        Dim i
        Dim objNS
 
        On Error Resume Next
        strFolderPath = Replace(FolderPath, "/", "\")
        aFolders = Split(FolderPath, "\")
 
        'get the Outlook objects
        ' use intrinsic Application object in form script
        objNS = Application.GetNamespace("MAPI")
 
        'set the root folder
        fldr = objNS.Folders(aFolders(0))
 
        'loop through the array to get the subfolder
        'loop is skipped when there is only one element in the array
        For i = 1 To UBound(aFolders)
            fldr = fldr.Folders(aFolders(i))
            'check for errors
            'If Err() <> 0 Then Exit Function
        Next
        GetMyFolder = fldr
 
        ' dereference objects
        objNS = Nothing
    End Function
End Class
  Permalink  

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

  Print Answers RSS
0 Maciej Los 450
1 OriginalGriff 403
2 Sergey Alexandrovich Kryukov 195
3 CPallini 189
4 CHill60 180
0 OriginalGriff 6,092
1 Sergey Alexandrovich Kryukov 4,958
2 Maciej Los 3,269
3 Peter Leow 3,129
4 DamithSL 2,490


Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 5 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100