Solved

Accessing public folders with VBA

Posted on 2000-03-20
5
328 Views
Last Modified: 2010-04-08
How can I access a public folder to populate a contact list?  I have added the following code
Set cf = OpenMAPIFolder("\Public Folders\All Public Folders\MIS\Test\Address Book")

but the contacts always end up in my default mailbox contact folder.
0
Comment
Question by:toboyle
  • 4
5 Comments
 
LVL 16

Expert Comment

by:Neo_mvps
ID: 2638470
You might like this function better. ;)

Function OpenMAPIFolder(ByVal strPath) As Outlook.MAPIFolder
    Dim objFldr As MAPIFolder
    Dim strDir As String
    Dim strName As String
    Dim i As Integer
    On Error Resume Next
    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

0
 
LVL 16

Expert Comment

by:Neo_mvps
ID: 2638550
Oops... fogot to add something.

Once you get your reference to the public folder from the function above or your own custom routine, you need to use the the Add method of the Items.  So your code becomes.

Set cf = OpenMAPIFolder ("...")
Set myContact = cf.items.add (olcontactitem)


I finished now.  ;)
0
 

Author Comment

by:toboyle
ID: 2642544
I added your function but the import still goes to my default contact folder, not the public folder.  I know I must be missing something.  Has you got your code to work?
0
 
LVL 16

Accepted Solution

by:
Neo_mvps earned 200 total points
ID: 2642782
The function that I supplied will get you a reference to a public folder that you ask for.  The "oops" part is what puts the item in the folder you specify.


So...


Set cf = OpenMAPIFolder("\Public Folders\All Public Folders\MIS\Test\Address Book")

Set myContact = cf.items.add (olcontactitem)
myContact.Display

Sorry about the answer being in two parts.

Note: Tested and works under OL2K

0
 
LVL 16

Expert Comment

by:Neo_mvps
ID: 2659607
I have one other idea for you.  The Contact Item has a method named Move.  You could easily save it and then w/out destroying the reference move the item to the public folder.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Outlook Free & Paid Tools
Follow this checklist to learn more about the 15 things you should never include in an email signature from personal quotes, animated gifs and out-of-date marketing content.
CodeTwo Sync for iCloud (http://www.codetwo.com/sync-for-icloud?sts=6554) automatically synchronizes your Outlook 2016, 2013, 2010 or 2007 folders with iCloud folders available via iCloud Control Panel. This lets you automatically sync them with…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question