Moving Contacts to another folder

G

Guest

Using Outlook 2003. There is a requirement at work where we move all
contacts from a particular folder to another folder on a regular basis. Is
it possible to induce "Select All" and then "Move to Folder" for Contacts in
Outlook via VBA for a folder? If not, which would be the quickest way to do
it instead of iterating through every contact item and then moving it.

I know the source folder and the target folder and only want to move the
contacts in the most efficient way. Looking forward to your suggestions.

Thanks in advance !
 
K

Ken Slovak - [MVP - Outlook]

You can copy/move an entire folder to another location, including contents.
But for what you want you'd have to iterate the Items collection of the
folder and move each item in turn, using a down counting loop.
 
G

Guest

Hi Ken,

Thank you for the quick reply.

Yes, that is what I am presently doing but find that the manual approach of
selecting all the contacts using Edit -> Select All in the folder and then
moving it to the target folder seems to be fractionally quicker (especially
when the number of contacts are large). Hence the question if its possible
to get a VBA equivalent of this using the command bars or something else.

Jonathan
 
K

Ken Slovak - [MVP - Outlook]

It's faster because you're using the Outlook object model, which calls
Extended MAPI and Outlook is directly using Extended MAPI. If you were to
code the copy operation using Extended MAPI it would be as fast as Outlook
is.
 
G

Guest

Ken,

Thanks for information. So can Extended MAPI be implemented using VBA or
VB? I did a search since this is a relatively new arena for me but from
initial impressions it appears can't be done with VBA or VB. Am I correct?

Another issue I have noticed is that the move seems to fire the
Birthday/Anniversary date event (if present) causing duplicates in the
Calendar. Any suggestions on how I can avoid this? Currently I am capturing
the Add event and deleting it. If there is another better approach, I'm all
ears.

Thanks once again !
Jonathan
 
K

Ken Slovak - [MVP - Outlook]

Extended MAPI can mostly only be programmed using C++ or Delphi. There are a
few properties you can read using MAPI from VBA but it's a hack and you
can't do complex things like moving items using VBA/VB.

You would have to do what you're doing for any reminders you don't want
replicated or clear the reminder flag on the item when you have it
instantiated and before you move it.
 
G

Guest

Hi,

I have pretty much got it doing what it has to. Except for one small issue.

I have a form with a button.. on the click of button the move between source
and target folder occurs. During this process I setup a flag (boolean
variable) to determine the Calendar "Add Event" (so that i can delete it). I
am also capturing the Deleted folder "Add Event" since I don't want it in the
deleted folder as well. All of this works well provided I don't unload the
form (Unload Me statement) at the end of the button click event. If I unload
the form then it fails to delete a couple of appointments (I guess its
because the app is shutting down before it can perform the lines of code).

So how would you suggest I go about solving this problem. Thought of using
a Do Loop While <condition> but have not had success with it since the
AddItem events for the two folders doesn't occur one after the other... there
seems to be a pattern of 2 to 3 AddItem for Calendar followed by 2 to 3
AddItem for Deletedfolder. Here is a cleaned up extract of the code:

Private Sub Move_Click() ' <- Move button to move the contacts
MoveAllContacts
Unload Me
End Sub

Sub MoveAllContacts()
CalendarDelete = True '<- flag to determine mass move of contacts is on
'... code to move the contacts
End Sub

Private Sub oCalendarFolder_ItemAdd(ByVal Item As Object)
If CalendarDelete = True Then
Item.Delete
End If
End Sub

Private Sub oDeletedFolder_ItemAdd(ByVal Item As Object)
If CalendarDelete = True Then
Item.Delete
End If
End Sub

Thanks!
Jonathan
 
K

Ken Slovak - [MVP - Outlook]

I'd probably set a flag when the synch code is completed and not run the
line that unloads the form until that flag is set. Given your conditions I'd
probably set the flag in the Deleted Items folder add event handler after
the newly deleted item was deleted from Deleted Items.
 
G

Guest

Hi Ken,

Thank you for your reply.

I did set a boolean exit flag. Set it to True when inside the add event of
Calendar folder and set it to false within the add event of Deleted folder.
But as I said earlier... this would work if the sequence of the events are
one after the
other.

What happens is that there are 2 or some times 3 add event for
Calendar and only then the add event of the Deleted folder takes place.. so
what happens is that the first add event of the deleted folder sets the Exit
flag to False and therefore when the condition is checked by Do Loop While
<condition> it is satisfied and therefore exits. Leaving the last one/two
items in the deleted folder. I hope I explained the scenario clearly.

I thought of adding a additional counter to keep track of the total Add
events for Calendar and then ensure that that many events has occured in the
Deleted folder Add event as well but this approach too gives strange
results. The counter doesn't add up to the same number and then hangs
in the Do loop. Not certain but I think this issue might be due to adding
of the items via the program. So ultimately my Do loop looks something like
this:

Do
Doevents
Loop While bExitloop = True And itemctr2 < itemctr1
Unload Me

bExitloop is set to True in the Add event of Calendar folder and False in
the Add event of Deleted folder. Itemctr1 and itemctr2 are counters within
each of these events that are incremented by 1.

Any help will be appreciated a lot.

Jonathan
 
G

Guest

oCalendarFolder is the default Calendar folder. To trap the
birthday/anniversary events that are generated while moving the Contacts.

Jonathan
 
K

Ken Slovak - [MVP - Outlook]

If that's all you want to catch, try this then. Get the item to move, check
to see whether or not those properties have values (a birthday or
anniversary that has no dates set reads out as 1/1/4501). If it has dates
there store them and clear the dates to None (1/1/4501). Make the move to
wherever you want. Then reset the birthday and anniversary fields.

That should provide you with a defined time frame when the ItemAdd event
will fire, after the move. See if that helps.
 

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