Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am writing the following code to save mails to inbox sub folder (Archive):

VB
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 ... :doh: :doh:
Posted
Updated 24-Aug-10 8:07am
v6

Make sure you have got the right namespaces included in your project.
 
Share this answer
 
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.
 
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