Working with non-default folders in VBA

G

Guest

I am trying to use VBA to write out a file, containing names and telephone
numbers contained in a non-default folder. It is a Contacts folder, in my
"Personal Folder" collection, called Hennepin, which exists as a subfolder
of the default Contacts folder. The routine attempts to write out all names
and business telephone numbers to a file called HennepinFile.txt. If and
when I can get this simple operation to work, I hope to do some more
complicated things with it. Unfortunately, It does not yet work.

I am an end user, not a programmer, and do not yet understand this very
well. The code I have so far is created from samples from this newgroup,
and Sue Mosher's very useful website, OutlookCode.com. It does not run, and
I think I am well out of my depth at this point. I would appreciate any
assistance. Thank you.

*******************************************
This is what I have so far -
*******************************************

' This function is lifted verbatim from Ms. Mosher's website,
www.outlookcode.com

Public Function GetFolder(strFolderPath As String) As MAPIFolder
strFolderPath = "Personal Folders\Contacts\Hennepin"

Dim objApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Dim colFolders As Outlook.Folders
Dim objFolder As Outlook.MAPIFolder
Dim arrFolders() As String
Dim I As Long
On Error Resume Next

strFolderPath = Replace(strFolderPath, "/", "\")
arrFolders() = Split(strFolderPath, "\")
Set objApp = CreateObject("Outlook.Application")
Set objNS = objApp.GetNamespace("MAPI")
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

Set GetFolder = objFolder
Set colFolders = Nothing
Set objNS = Nothing
Set objApp = Nothing
End Function

Sub HennepinList()

Dim oApp As Outlook.Application
Dim oNspc As NameSpace
Dim oItm As ContactItem
Dim ContactName As String
Dim FS As Object
Dim mynewfile As Object


Set FS = CreateObject("Scripting.FileSystemObject")
Set mynewfile = FS.CreateTextFile("c:\temp\Hennepinfile.txt", True)
Set oApp = CreateObject("Outlook.Application")
Set oNspc = oApp.GetNamespace("MAPI")

' the lines below are currently where the problem is

For Each oItm In oNspc.GetFolder _
(olFolderContacts).Items

If Not oItm.BusinessTelephoneNumber = "" Then

ContactName = oItm.FileAs
ContactName = ContactName + " " + oItm.BusinessTelephoneNumber
mynewfile.writeline (ContactName)

End If

Next oItm

Set oItm = Nothing
Set oNspc = Nothing
Set oApp = Nothing
mynewfile.Close

End Sub
 
S

Sue Mosher [MVP-Outlook]

Well, if you're working with VBA code, you're now a programmer.
Congratulations.

oNspc.GetFolder is not a valid statement. When in doubt, check the object
browser: Press ALt+F11 to open the VBA environment in Outlook, then press
F2. Switch from <All Libraries> to Outlook to browse all Outlook objects and
their properties, methods, and events. You'll see that the Namespace object
has no GetFolder method.

The correct usage of the GetFolder function that you've inlcuded in your
code would be:

Set objFolder = GetFolder("Personal Folders\Contacts\Hennepin")

Take this statement out of the GetFolder function:

strFolderPath = "Personal Folders\Contacts\Hennepin"

You do not set the folder path in the GetFolder function, but pass it as an
argument.

Once you have the folder, you can loop through it:

For Each objItem in objFolder.Items
' do soemthing with objItem
next

FYI, there is a newsgroup specifically for general Outlook programming
issues "down the hall" at microsoft.public.outlook.program_vba or, via web
interface, at
http://www.microsoft.com/office/community/en-us/default.mspx?dg=microsoft.public.outlook.program_vba

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
G

Guest

Thank you for taking the time to help out. With the modifications you
pointed out, the thing now runs perfectly. One small step . . .
 

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