Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET Outlook
I am writing the following code to save mails to inbox sub folder (Archive):

Sub MoveOldEmail()
Dim oItem As MailItem
Dim objMoveFolder As MAPIFolder
Dim objInboxFolder As MAPIFolder
Dim i As Integer
 
    Set objMoveFolder = GetFolder("Personal Folders\Inbox\Archive")
    Set objInboxFolder = GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
 
    For i = objInboxFolder.Items.Count - 1 To 0 Step -1
 
        With objInboxFolder.Items(i)
 
            ''Error 438 is returned when .receivedtime is not supported            
            On Error Resume Next
 
            If .ReceivedTime < DateAdd("h", -24, Now) Then
                If Err.Number = 0 Then
                    .Move objMoveFolder
                Else
                    Err.Clear
                End If
            End If
        End With
 
    Next
 
    Set objMoveFolder = Nothing
    Set objInboxFolder = Nothing
 
End Sub
 
Public Function GetFolder(strFolderPath As String) As MAPIFolder
'' strFolderPath needs to be something like
''   "Public Folders\All Public Folders\Company\Sales" or
''   "Personal Folders\Inbox\My Folder"

Dim objNS As NameSpace
Dim colFolders As Folders
Dim objFolder As MAPIFolder
Dim arrFolders() As String
Dim i As Long
 
On Error GoTo TrapError
 
    strFolderPath = Replace(strFolderPath, "/", "\")
    arrFolders() = Split(strFolderPath, "\")
 
    Set objNS = GetNamespace("MAPI")
 
    On Error Resume Next
 
    Set objFolder = objNS.Folders.Item(arrFolders(0))
 
    If Not objFolder Is Nothing Then
        For i = 1 To UBound(arrFolders)
            Set colFolders = objFolder.Folders
            Set objFolder = Nothing
            Set objFolder = colFolders.Item(arrFolders(i))
 
            If objFolder Is Nothing Then
                Exit For
            End If
        Next
    End If
 
On Error GoTo TrapError
 
    Set GetFolder = objFolder
    Set colFolders = Nothing
    Set objNS = Nothing
 
Exit_Proc:
    Exit Function
 
TrapError:
    MsgBox Err.Number & " " & Err.Description
 
End Function
 
But I am getting the following errors:
 
Error	1	Name 'GetNamespace' is not declared.	D:\MIT\project\Outlook 2007\WindowsApplication1\WindowsApplication1\Form1.vb	17	26	WindowsApplication1
Error	2	Name 'olFolderInbox' is not declared.	D:\MIT\project\Outlook 2007\WindowsApplication1\WindowsApplication1\Form1.vb	17	64	WindowsApplication1
Error	3	Number of indices is less than the number of dimensions of the indexed array.	D:\MIT\project\Outlook 2007\WindowsApplication1\WindowsApplication1\Form1.vb	56	19	WindowsApplication1
Error	4	Name 'GetNamespace' is not declared.	D:\MIT\project\Outlook 2007\WindowsApplication1\WindowsApplication1\Form1.vb	58	17	WindowsApplication1
 
Please tell me how I can recover it ... D'Oh! | :doh: D'Oh! | :doh:
Posted 12-Jun-10 18:49pm
Edited 24-Aug-10 9:07am
v6
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Make sure you have got the right namespaces included in your project.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Error 1 & 4 Name 'GetNamespace' is not declared. D:\MIT\project\Outlook 2007\WindowsApplication1\WindowsApplication1\Form1.vb 17 26 WindowsApplication1
Very correct! This method is exposed in the Outlook object. The way you are trying to use it looks like a normal page method. Create Outlook object and then try.
 
Error 2 Name 'olFolderInbox' is not declared. D:\MIT\project\Outlook 2007\WindowsApplication1\WindowsApplication1\Form1.vb 17 64 WindowsApplication1
I don't find them defined. They are fixed integers that are used in Outlook to find an item.
 
Refer this article [^]for some reference. Though this is in client side in Javascript, but you will get the essence of what are the issues with Error 1 & 2 & 4
 
Error 3 Number of indices is less than the number of dimensions of the indexed array. D:\MIT\project\Outlook 2007\WindowsApplication1\WindowsApplication1\Form1.vb 56 19 WindowsApplication1
Sounds like a coding error. Just use debugger and then resolve the same.
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 575
1 Kornfeld Eliyahu Peter 409
2 Maciej Los 369
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,353
1 DamithSL 4,854
2 Maciej Los 4,466
3 Kornfeld Eliyahu Peter 4,058
4 Sergey Alexandrovich Kryukov 3,897


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 21 Sep 2011
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