Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

test if mailbox exists

Status
Not open for further replies.

easyit

Programmer
Aug 22, 2003
443
NL
Hi,

I use the following to retrieve all email entries in a mailbox on the exchange server:

Code:
 Function RecursiveFolderSearch() As Boolean
    On Error GoTo Error_Handler
    Dim i As Integer
    Dim MyMapiFolder As Outlook.MAPIFolder
    Dim conn As New adodb.Connection
    Dim rst As New adodb.Recordset
    Dim PostbusNaam, teRapporterenMapNaam As String
    
    RecursiveFolderSearch = True

    Forms!frmfeedback.txtFocus.SetFocus
    
    Set OutlookApp = New Outlook.Application
    Set myNameSpace = OutlookApp.GetNamespace("MAPI")
    
    Set conn = CurrentProject.Connection
    rst.Open ("MAIL_tbl_teams"), conn 'open de tabel met alle postbusnamen
    rst.MoveFirst ' ga naar de eerste postbus
    While Not rst.EOF ' verwerk alle postbussen
    PostbusNaam = rst.Fields("PostbusNaam") 'haal de eerste postbus op.
    verplaatsMail = rst.Fields("vandaag_verwerkt_legen")
    If rst!meenemen Then
        teRapporterenMapNaam = rst!rootfolder
        Forms!frmfeedback.txtonderdeel = PostbusNaam
        Forms!frmfeedback.Repaint
retry:
        Set MyMapiFolder = myNameSpace.Folders(PostbusNaam).Folders(teRapporterenMapNaam)
        Set CurrentMailBox = myNameSpace.Folders(PostbusNaam)
        Call GetFolderNames(MyMapiFolder, rst.Fields("TeamNaam"))
    End If
    rst.MoveNext
    Wend

Exit_Handler:
    Set myNameSpace = Nothing
    Set MyMapiFolder = Nothing
    Exit Function

Error_Handler:
    If Err.Number = -2147221233 Then 
        If InStr(1, PostbusNaam, "Mailbox") Then
            PostbusNaam = Replace(PostbusNaam, "Mailbox", "Postbus", , , vbTextCompare)
        Else
            PostbusNaam = Replace(PostbusNaam, "Postbus", "Mailbox", , , vbTextCompare)
        End If
        GoTo retry:
    End If
    RecursiveFolderSearch = False
    Forms!frmimportexport.btnFoutenLog.Enabled = True
    Call LogError("Fout in functie RecursiveFolderSearch. " & PostbusNaam & " Fout:", CurrentUser(), (Err.Number & " " & Err.Description & " "))
    GoTo Exit_Handler
End Function
Works fine, except when PostbusNaam contains a name that doesn't exist (typos happen, because users can add names). There are two reasons for a mailbox that doesn't exists;
1) the mailbox is localized (ie "Postbus" instead of "Mailbox" (we use different servers on the corporate network). For this case I trap the generated error number (-2147221233) and replace "Mailbox" in the string with "Postbus".
2) The mailbox doesn't exist at all.

I need to signal this, but cannot figure out how to test this, because I can't discriminate the "why".
If I retry to set with the changed name ("Postbus" instead of "Mailbox") and this is the correct error reason it works fine. If not however, it jumps straight out of the function, without generating a error.

Does anybody know how to correctly set up this?

EasyIT
 
Sorry, got it solved within a few minutes after posting. Sometimes you just need to write it down...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top