Save method does not save changes

E

ErdoesiT

Hi,

I am trying write a VBA script that to copies a ContactItem and then
modifies the original item and its copy. Unfortunately the changes to
the source item are ignored.

The following piece of code can be used to reproduce this behaviour:


Public Sub Test()
Dim Namespace As Namespace
Dim Folder As MAPIFolder

Dim SourceItem As ContactItem
Dim TargetItem As ContactItem

Set Namespace = GetNamespace("MAPI")
Set Folder = Namespace.GetDefaultFolder(olFolderContacts)

'Copy first item in default contacts folder
Set SourceItem = Folder.Items(1)
Set TargetItem = SourceItem.Copy

'Modify source and target item
SourceItem.Body = "TargetItem: " & TargetItem.EntryID
TargetItem.Body = "SourceItem: " & SourceItem.EntryID

'Save changes
SourceItem.Save
TargetItem.Save
End Sub


Can anybody there tell me what's wrong with this script?

Thomas
 
S

Sue Mosher [MVP-Outlook]

Make sure you dererence all objects by setting them to Nothing before you try to open them in the UI.
 
E

ErdoesiT

Hi Sue,

Thanks for your reply. I added the following lines but unfortunately
this did not change the behaviour of the coding. :-(

Set SourceItem = Nothing
Set TargetItem = Nothing
Set Folder = Nothing
Set Namespace = Nothing

Thomas
 
E

ErdoesiT

Finally I found a workaround to solve the problem:


Public Sub Test()
Dim Namespace As Namespace
Dim Folder As MAPIFolder

Dim SourceItem As ContactItem
Dim TargetItem As ContactItem

Set Namespace = GetNamespace("MAPI")
Set Folder = Namespace.GetDefaultFolder(olFolderContacts)

Set SourceItem = Folder.Items(1)
Set TargetItem = SourceItem.Copy
Set SourceItem = Nothing
Set SourceItem = Folder.Items(1)

SourceItem.Body = "TargetItem: " & TargetItem.EntryID
TargetItem.Body = "SourceItem: " & SourceItem.EntryID

SourceItem.Save
TargetItem.Save
End Sub


It seems that a ContactItem becomes unusable after calling the
Copy-Method.
Unfortunately this means that I have to change a lot of coding because
I cannot use a Collection to store ContactItems...

Of course Microsoft will be able to explain why this ist not a bug but
a feature...
 

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