Solved

Accessing public folders with VBA

Posted on 2000-03-20
5
329 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Are you unable to connect or configure Hotmail email account in Microsoft Outlook 2010, 2007? Or Outlook.com emails are not downloading to Outlook? Lets’ see the problem and resolve Outlook Connector error syncing folder hierarchy (0x8004102A).
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

828 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