Link to home
Start Free TrialLog in
Avatar of RentANerds
RentANerds

asked on

Outlook 2007 - VBA to copy Public Folder contacts between folders and clear certain fields

Im looking for a VBA script that can be run from a Windows XP (with Office 2007) workstation that will do the following:

1  Erase all the contact items under the Public Folder called Rolodex (but not the folder itself)
2  Copy all the contact items from the Public Folder called Rolodex-Private to Rolodex
3  Erase the following fields from the contacts in Rolodex:  Category, Home Address & Notes (or simply not copy those fields)

Ive seen a few different solutions online that do similar things but nothing that comes close enough for me, with my limited programming skills, to figure out how to adapt for my uses.

Im under the gun with several projects at once here and hopefully someone will have a complete & easy solution for this!


(The most similar solution Ive found is https://www.experts-exchange.com/questions/22125021/Outlook-2003-VBA-to-copy-Public-Folder-Contacts-to-Mailbox-Contacts-subfolder.html but it deletes the entire folder instead of just the contents and also doesnt show how to erase data from specific contact fields)
ASKER CERTIFIED SOLUTION
Avatar of David Lee
David Lee
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RentANerds
RentANerds

ASKER

The code worked FANTASTIC on the first try!  Thank-you VERY MUCH FOR IT!
You're welcome.  Happy I could help.
Hello, this script work for me to but i don't want to delete the contacts in the target map, because i want to overwrite them, becaue i have no scripting knowledge how do you change the script that way that the contacts from the target folder will me overwrited instead of deleted?
I'm not sure if this will overwrite or duplicate ... and don't have time to test this morning...  (my gut feeling is that it may the duplicate)

But here is the same code that simply lacks the delete portion.  Let us know what happens.
Sub CopyPFContacts()
    Dim olkSource As Outlook.Folder, _
        olkTarget As Outlook.Folder, _
        olkItem As Outlook.ContactItem, _
        olkCopy As Outlook.ContactItem, _
        intIndex As Integer
    Set olkSource = Session.GetDefaultFolder(olPublicFoldersAllPublicFolders).Folders("Rolodex-Private")
    Set olkTarget = Session.GetDefaultFolder(olPublicFoldersAllPublicFolders).Folders("Rolodex")
    'Copy all items from the source folder to the target folder while removing the requested fields'
    For Each olkItem In olkSource.Items
        Set olkCopy = olkItem.Copy
        With olkCopy
            .Categories = ""
            .HomeAddress = ""
            .Body = ""
            .Save
            .Move olkTarget
        End With
    Next
    Set olkSource = Nothing
    Set olkTarget = Nothing
    Set olkItem = Nothing
    Set olkCopy = Nothing
    MsgBox "Finished.", vbInformation + vbOKOnly, "Copy Public Folder Contacts"
End Sub

Open in new window

when i execute that script, and i have a contact named test in both folders, the script don´t remove the old test in the target folder