Moving/copying contacts to a different folder fails

  • Thread starter Thread starter David Kleyman
  • Start date Start date
D

David Kleyman

Hello

I am trying to copy contacts from the default folder to a new one
I am using VB.NET and Outlook 2003
but it fails with this error
Error number 1248722935 Error description "Can't move the items."

Here is the code that does it
Could anybody take a look at it and let me know what's wrong?

Sub CopyItems(ByRef olApp As Microsoft.Office.Interop.Outlook.Application)

Dim olNS As Microsoft.Office.Interop.Outlook.NameSpace

Dim olSourceFolder, olTargetFolder As
Microsoft.Office.Interop.Outlook.MAPIFolder

Dim olMonticelloFolder As Microsoft.Office.Interop.Outlook.MAPIFolder

Dim items As Microsoft.Office.Interop.Outlook.Items

Dim item As Microsoft.Office.Interop.Outlook.ContactItem

Dim itemNew As Microsoft.Office.Interop.Outlook.ContactItem

Dim i As Short

Try

olNS = olApp.GetNamespace("MAPI")

olSourceFolder =
olNS.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFo
lderContacts)

For i = 1 To olNS.Folders.Count

If LCase(olNS.Folders.Item(i).Name) = "monticello" Then

olTargetFolder = olNS.Folders(i).Folders.Item("Monticello")

Exit For

End If

Next i

items = olSourceFolder.Items

If items.Count = 0 Then

MsgBox("No contacts to export")

Exit Sub

End If



For Each item In items

itemNew = CType(item.Copy, Microsoft.Office.Interop.Outlook.ContactItem)

itemNew.MessageClass = "IPM.Contact.MonticelloContact"

itemNew.Save()

itemNew.Move(olTargetFolder)

Next

MsgBox("All items exported!")

Catch ex As SystemException

MsgBox("Main.CopyItems - " & "Error #" & Err.Number & "-" & Err.Description,
MsgBoxStyle.Critical + MsgBoxStyle.OKOnly, "Monticello")

DebugWriter("Main.CopyItems", Err.Description)

End Try

End Sub



Thanks for your help



-David
 
Your code doesn't seem to have any statement that checks whether
olTargetFolder exists. This code seems to look for a top-level infostore
named "monticello" and then assigns the olTargetFolder object to the
Monticello folder inside the monticello infostore:

If LCase(olNS.Folders.Item(i).Name) = "monticello" Then
olTargetFolder = olNS.Folders(i).Folders.Item("Monticello")

Does such a folder actually exist -- I presume in a .pst file with the
display name monticello?
 
Well, then your code is looking for the folder in the wrong place, isn't it?
You'll need to change your code to walk the folder hierarchy to get to that
folder under contacts. To get a non-default folder, you need to walk the
folder hierarchy using the Folders collections or use a function that does
that for you. See http://www.outlookcode.com/d/code/getfolder.htm

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
That worked
Thank you very much

-David Kleyman

Sue Mosher said:
Well, then your code is looking for the folder in the wrong place, isn't it?
You'll need to change your code to walk the folder hierarchy to get to that
folder under contacts. To get a non-default folder, you need to walk the
folder hierarchy using the Folders collections or use a function that does
that for you. See http://www.outlookcode.com/d/code/getfolder.htm

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
Back
Top