Adding a new contact via vba fails

D

Dave

Trying to add a new contact with the following code. If Outlook is already
open then this code works well. If Outlook is not open then this code fails
"Method 'Save' of object '_ContactItem' failed". Error # -2147417851

Dim olApp As Outlook.Application
Set olApp = New Outlook.Application
Dim olItem As Outlook.ContactItem
Set olItem = olApp.CreateItem(olContactItem)
With olItem
.FullName = "John Doe"
.Birthday = "6/30/1980"
.HomeTelephoneNumber = "800-555-1212"
.Email1Address = "(e-mail address removed)"
End With

'this is what fails
olItem.Save

Dave
 
M

Michael Bauer

Hi Dave,

I won´t believe but you are right. If someone could explain, I´m
interested, too.

A workaround: Create the item by adding it into the folder directly,
e.g.:

Set olItem=olApp.Session.GetDefaultFolder(olFolderContacts).Items.Add
 
D

Dave

Sue,
I get the same failed results when using your code sample.

ps. see my response to Michael Bauer this topic. His solution works.
Thks,
Dave
 
M

Michael Bauer

Hi Sue,

I´ve tested with OL2k, Namespace.Logon doesn´t matter. After the error
occurs, OL stays in the task list, regardless of using App.Quit etc.

Do you know anything about this?
 
S

Sue Mosher [MVP-Outlook]

CW or IMO? My recollection is that they act differently.

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

Michael Bauer

CW

--
Viele Grüße
Michael Bauer


Sue Mosher said:
CW or IMO? My recollection is that they act differently.

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

Sue Mosher [MVP-Outlook]

This is what I typically use:

On Error Resume Next
Set objOL = GetObject(, "Outlook Application")
If objOL Is Nothing Then
Set objOL = CreateObject("Outlook.Application")
Set objNS = objOL.GetNamespace("MAPI")
objNS.Logon "name of profile"

' do some stuff

' close all Inspector and Explorer windows

objNS.Logoff
objOL.Quit
End If

Outlook 2000 can take a long time -- minutes -- for its MAPI spooler to shut
down.

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

Michael Bauer

Hi Sue,

thanks, your code works for me without errors, too. Obviously the error
causing difference is that I´ve used Application.Session.Logon instead
of the GetNamespace method.

Do you know why this makes a difference?
Outlook 2000 can take a long time -- minutes -- for its MAPI spooler to shut
down.

The main problem was, that the ContactItem wasn´t be saved. But with
GetNamespace.Logon everything is fine.
 
S

Sue Mosher [MVP-Outlook]

Sure don't. I was always told to use Namespace, not Application.Session,
though.

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

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