none
Outlook 2010 - Copy a contact list to a contact list in the public folder RRS feed

  • Question

  • I have make a script for copy a contact list to a contact list in the public folder but if i'm not on the contact source , the macro don't work, i don't know why, thx.

    I things the error come from:
    Set objItem = Application.ActiveExplorer.Selection.Item(1)


    Sub Movecopycontacts()
        
        Dim objOutlook As Outlook.Application
        Dim objNamespace As Outlook.NameSpace
        Dim objSourceFolder As Outlook.MAPIFolder
        Dim objDestFolder As Outlook.MAPIFolder
        Dim objItem As ContactItem
          
        Set objOutlook = Application
        Set objNamespace = objOutlook.GetNamespace("MAPI")
        Set objSourceFolder = objNamespace.GetDefaultFolder(olFolderContacts)
        Set objItem = Application.ActiveExplorer.Selection.Item(1)
        Set objDestFolder = objNamespace.Folders("Public folder - oky@test.com").Folders("all public folder").Folders("test")
      
        objItem.Move objDestFolder
                    
        Set objDestFolder = Nothing
    End Sub
    Monday, August 10, 2015 1:48 PM

Answers

  • If you want to copy the items to another folder you must copy first and then move the copies, however assuming your objDestFolder points to the correct folder, then:

    Sub CopyContacts()
    Dim objNamespace As Outlook.NameSpace
    Dim objSourceFolder As Outlook.MAPIFolder
    Dim objDestFolder As Outlook.MAPIFolder
    Dim objItem As ContactItem
    Dim objCopyItem As ContactItem
    
        Set objNamespace = Outlook.GetNamespace("MAPI")
        Set objSourceFolder = objNamespace.GetDefaultFolder(olFolderContacts)
        Set objDestFolder = objNamespace.folders("Public folder - oky@test.com").folders("all public folder").folders("test")
        For Each objItem In objSourceFolder.Items
            Set objCopyItem = objItem.Copy
            objCopyItem.Move objDestFolder
        Next objItem
        Set objDestFolder = Nothing
        Set objSourceFolder = Nothing
        Set objNamespace = Nothing
        Set objItem = Nothing
        Set objCopyItem = Nothing
    End Sub
    should be nearer the mark.


    Graham Mayor - Word MVP
    www.gmayor.com

    • Marked as answer by Bartoch Tuesday, August 11, 2015 9:45 AM
    Tuesday, August 11, 2015 7:41 AM

All replies

  • If you want to copy the items to another folder you must copy first and then move the copies, however assuming your objDestFolder points to the correct folder, then:

    Sub CopyContacts()
    Dim objNamespace As Outlook.NameSpace
    Dim objSourceFolder As Outlook.MAPIFolder
    Dim objDestFolder As Outlook.MAPIFolder
    Dim objItem As ContactItem
    Dim objCopyItem As ContactItem
    
        Set objNamespace = Outlook.GetNamespace("MAPI")
        Set objSourceFolder = objNamespace.GetDefaultFolder(olFolderContacts)
        Set objDestFolder = objNamespace.folders("Public folder - oky@test.com").folders("all public folder").folders("test")
        For Each objItem In objSourceFolder.Items
            Set objCopyItem = objItem.Copy
            objCopyItem.Move objDestFolder
        Next objItem
        Set objDestFolder = Nothing
        Set objSourceFolder = Nothing
        Set objNamespace = Nothing
        Set objItem = Nothing
        Set objCopyItem = Nothing
    End Sub
    should be nearer the mark.


    Graham Mayor - Word MVP
    www.gmayor.com

    • Marked as answer by Bartoch Tuesday, August 11, 2015 9:45 AM
    Tuesday, August 11, 2015 7:41 AM
  • thx a lot !
    Tuesday, August 11, 2015 9:45 AM