delete a folder from my contacts in Outlook VBA

G

Guest

I am using VBA to create and delete folders in the default contact folder.
This seems to be working fine - the new folder is always created and the
items that I generate are included in the new folder.

The problem is that when I go into the Contacts folder in Outlook Explorer I
still see the old folder in My Contacts. When I click on the old entry
Explorer says that the folder has been deleted. So how do I remove the
reference to this folder from My Contacts.

The old folder also persists in the list of folders to be used as an address
book.
 
G

Guest

Strange, I can't reproduce this at all. Can you show your code? What is
your full Outlook version and build number?
 
G

Guest

Thank you for looking at this for me.

I am running Outlook 2003 (11.6568.6568) SP2

Here is a version of the code:
Dim ol_app As New Outlook.Application
Dim ol_namespace As Outlook.Namespace
Dim ol_folder_contacts As Outlook.MAPIFolder
Dim ol_folder_volunteers As Outlook.MAPIFolder
Dim ol_xplorer As Outlook.Explorer
Dim ol_dist_list As Outlook.DistListItem
Dim ol_recipient As Outlook.Recipient
Dim ol_temp_mail As Outlook.MailItem
Dim sycs As Outlook.SyncObjects
Dim syc As Outlook.SyncObject
Dim rs As New ADODB.Recordset
Dim first_up As Boolean
Dim prevGroup As String
Dim i As Long

DoCmd.Hourglass True
Set ol_namespace = ol_app.GetNamespace("MAPI")
Set ol_folder_contacts = ol_namespace.GetDefaultFolder(olFolderContacts)

For i = 1 To ol_folder_contacts.Folders.Count
If ol_folder_contacts.Folders(i) = "Volunteers" Then
Set ol_folder_volunteers = ol_folder_contacts.Folders(i)
ol_folder_volunteers.ShowAsOutlookAB = False
Set ol_folder_volunteers = Nothing
ol_folder_contacts.Folders.Remove i
Exit For
End If
Next

Set ol_folder_volunteers = ol_folder_contacts.Folders.Add("Volunteers")
ol_folder_volunteers.ShowAsOutlookAB = True



Note that the code is acutally running in Access - not Outlook. What I am
trying to acheive is having my Access App generate a new Volunteers folder
and populate it with a contacts.

As I said in the original post - everything seems to be working except that
the old folder still appears under My Contacts.

Usually I do not have Outlook Explorer running while I run the code in
Access. So I first see the problem when I launch the Outlook and goto the
Contacts Folder.

Another aspect of the problem is that it appears to correct itself over
time. Eventually the old folder will disappear. It usually take about 30
minutes to do so - with Outlook explorer not active.

Again - thanks for looking at this.

Sean
 
G

Guest

Thanks for looking at this. I re-posted a fuller description of the problem
in a reponse to Eric's post.

When I tried what you suggested I errored out. This is because I don't have
an exlporer active at the time that I run the code in Access.

Sean
 
S

Sue Mosher [MVP-Outlook]

So, what happens if you show an Explorer and perform the operation, then close it?

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
G

Guest

More bad stuff happens. I get a runtime error C2004005 saying that "The
folder selections was not cleared. At least one folder must remain selected.

I added extra code to select another folder prior to the deselect. I could
see that this folder was selected, and it's contents displayed. But I still
got the same error.

Sean
 
S

Sue Mosher [MVP-Outlook]

How about showing a code snippet that others can try?

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
G

Guest

There is sample code in my reply to Eric.

Sean

Sue Mosher said:
How about showing a code snippet that others can try?

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
S

Sue Mosher [MVP-Outlook]

But that doesn't show deselecting the folder in an Explorer. I'd try showing the user's Contacts folder in an Explorer, deselecting the folder you're interested in, and then closing the Explorer.

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
G

Guest

Private Sub GenerateOutlookCmd_Click()
Dim ol_app As New Outlook.Application
Dim ol_namespace As Outlook.Namespace
Dim ol_folder_contacts As Outlook.MAPIFolder
Dim ol_folder_volunteers As Outlook.MAPIFolder
Dim ol_xplorer As Outlook.Explorer
Dim ol_dist_list As Outlook.DistListItem
Dim ol_recipient As Outlook.Recipient
Dim ol_temp_mail As Outlook.MailItem
Dim sycs As Outlook.SyncObjects
Dim syc As Outlook.SyncObject
Dim rs As New ADODB.Recordset
Dim first_up As Boolean
Dim prevGroup As String
Dim i As Long

DoCmd.Hourglass True
Set ol_namespace = ol_app.GetNamespace("MAPI")
Set ol_folder_contacts = ol_namespace.GetDefaultFolder(olFolderContacts)

Set ol_xplorer = ol_folder_contacts.GetExplorer
ol_xplorer.Activate

For i = 1 To ol_folder_contacts.Folders.Count
If ol_folder_contacts.Folders(i) = "Volunteers" Then
Set ol_folder_volunteers = ol_folder_contacts.Folders(i)
ol_folder_volunteers.ShowAsOutlookAB = False
ol_xplorer.DeselectFolder ol_folder_volunteers
Set ol_folder_volunteers = Nothing
ol_folder_contacts.Folders.Remove i
Exit For
End If
Next
 
S

Sue Mosher [MVP-Outlook]

How did ShowAsOutlookAB get into this equation instead of Explorer.DeselectFolder? I thought we were talking about the Contacts navigation pane?

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 

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