how to find outlook folder

E

Ed

I'm trying to use vba to write a macro that will goto the undeliverables
folder and open each item in it and search for a string.

I can't seem to connect to the undeliverables folder.
Here is a snippet of the code I am using:
---------------
Sub SearchUndelsFolder()
' Dim objRsts As Results
'establish two variables
Dim n As Integer
n = 0
Dim address As String

'create output file system object
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile("c:\FailedAddresses.txt", True)

'call the Outlook application MAPI interface
Set myOlApp = CreateObject("Outlook.Application")
Set myNamespace = myOlApp.GetNamespace("MAPI")

'Address the Undeliverables folder
Set myFolder = myNamespace.GetFolder("undeliverables")

'count entries in undeliverables folder
Set filecount = myFolder.Items.Count

'for each message body in the MAPI undeliverables subdirectiry
'address the message body of the nth entry in MyFolder

------------------
the code is failing at the MAPI call. Am I missing somethiing? The macro
crashed when it gets to the MAPI call.
I get this error message:
runtime error 1163395067 (baa80005)
the progrrm for the attachment may not have been installed lproperly or may
have been moved or deleted. reinstall the hprogram in hhich the attachment
was created.

I don't think I'm calling access to the undeliverables folder properly, but
can't figure out the proper call. Can you help?
Ed
 
E

Eric Legault [MVP - Outlook]

There is no GetFolder method! There are three ways to get a MAPIFolder object:

- use the Namespace.GetFolderFromID method if you know the ID of the folder
- "walk" the Namespace.Folders collection until you get the folder you want
(e.g. myFolder =
myNamespace.Folders("Inbox").Folders("SubFolder").Folders("AnotherSubFolder")
- use the function below if you know the path to the folder

'******************************************************************************
'Custom procedure: OpenMAPIFolder(ByVal strPath)
'Purpose: Return a MAPIFolder from Path argument
'Returns: MAPIFolder object
'******************************************************************************
Function OpenMAPIFolder(ByVal strPath) As Outlook.MAPIFolder
On Error Resume Next

Dim objFldr As MAPIFolder
Dim strDir As String
Dim strName As String
Dim i As Integer
If Left(strPath, Len("\")) = "\" Then
strPath = Mid(strPath, Len("\") + 1)
Else
Set objFldr = Application.ActiveExplorer.CurrentFolder
End If
While strPath <> ""
i = InStr(strPath, "\")
If i Then
strDir = Left(strPath, i - 1)
strPath = Mid(strPath, i + Len("\"))
Else
strDir = strPath
strPath = ""
End If
If objFldr Is Nothing Then
Set objFldr = Application.GetNamespace("MAPI").Folders(strDir)
On Error GoTo 0
Else
Set objFldr = objFldr.Folders(strDir)
End If
Wend
Set OpenMAPIFolder = objFldr
End Function
 
D

DENNIS BROWN

Great!

--

Thanks,
Dennis
There is no GetFolder method! There are three ways to get a MAPIFolder object:

- use the Namespace.GetFolderFromID method if you know the ID of the folder
- "walk" the Namespace.Folders collection until you get the folder you want
(e.g. myFolder =
myNamespace.Folders("Inbox").Folders("SubFolder").Folders("AnotherSubFolder")
- use the function below if you know the path to the folder

'******************************************************************************
'Custom procedure: OpenMAPIFolder(ByVal strPath)
'Purpose: Return a MAPIFolder from Path argument
'Returns: MAPIFolder object
'******************************************************************************
Function OpenMAPIFolder(ByVal strPath) As Outlook.MAPIFolder
On Error Resume Next

Dim objFldr As MAPIFolder
Dim strDir As String
Dim strName As String
Dim i As Integer
If Left(strPath, Len("\")) = "\" Then
strPath = Mid(strPath, Len("\") + 1)
Else
Set objFldr = Application.ActiveExplorer.CurrentFolder
End If
While strPath <> ""
i = InStr(strPath, "\")
If i Then
strDir = Left(strPath, i - 1)
strPath = Mid(strPath, i + Len("\"))
Else
strDir = strPath
strPath = ""
End If
If objFldr Is Nothing Then
Set objFldr = Application.GetNamespace("MAPI").Folders(strDir)
On Error GoTo 0
Else
Set objFldr = objFldr.Folders(strDir)
End If
Wend
Set OpenMAPIFolder = objFldr
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top