A
AnnieB
To the kind souls who offer hope...
I would like to create a button on a form in Access that allows the user to
send newly entered client contact data straight to Outlook.
Elsewhere on MSDN I found code that would open a new Outlook Contact Form
for data entry and also code that would send a new appointment with reminder
etc. to Outlook. I have created a testing DB and experimented with each of
them separately - references were set to Outlook 11 object library and all
worked well. I thought I might be able to adapt both to create what I wanted.
After the first try with the code below I received the error message "94 -
invalid use of null" so then I checked things like 'zero length string
allowed' and played with a couple of other things and now I am getting "Error
438 - Object does not support this property or method."
This is the code I have tried:
Private Sub AddContact_Click()
On Error GoTo AddContact_Err
' Save record first to be sure required fields are filled.
DoCmd.RunCommand acCmdSaveRecord
' Exit the procedure if Contact has been added to Outlook.
If Me!AddedToOutlook = True Then
MsgBox "This Contact has already been added to Microsoft Outlook"
Exit Sub
' Add a new contact.
Else
Dim outobj As Outlook.Application
Dim outcontact As Outlook.ContactItem
Set outobj = CreateObject("outlook.application")
Set outcontact = outobj.CreateItem(olContactItem)
With outcontact
.FirstName = Me!ContactFirst
.LastName = Me!ContactLast
.JobTitle = Me!JobTitle
.CompanyName = Me!Company
.BusinessTelephoneNumber = Me!BusinessPhone
If Not IsNull(Me!BusinessFax) Then .BusinessFaxNumber =
Me!BusinesFax
If Not IsNull(Me!Mobile) Then .MobileTelephoneNumber =
Me!Mobile
If Not IsNull(Me!Email) Then .Email1Address = Me!Email
If Not IsNull(Me!Address) Then .BusinessAddress = Me!Address
If Not IsNull(Me!City) Then .BusinessAddressCity = Me!City
If Not IsNull(Me!State) Then .BusinessAddressState = Me!State
If Not IsNull(Me!Postcode) Then .BusinessAddressPostalCode =
Me!Postcode
If Not IsNull(Me!Country) Then .BusinessAddressCountry =
Me!Country
If Not IsNull(Me!Notes) Then .Notes = Me!Notes
.Save
End With
End If
' Release the Outlook object variable.
Set outobj = Nothing
' Set the AddedToOutlook flag, save the record, display a message.
Me!AddedToOutlook = True
DoCmd.RunCommand acCmdSaveRecord
MsgBox "Contact Added!"
Exit Sub
AddContact_Err:
MsgBox "Error " & Err.Number & vbCrLf & Err.Description
Exit Sub
End Sub
If anyone has some suggestions or proven code I would be extremely grateful!
I would like to create a button on a form in Access that allows the user to
send newly entered client contact data straight to Outlook.
Elsewhere on MSDN I found code that would open a new Outlook Contact Form
for data entry and also code that would send a new appointment with reminder
etc. to Outlook. I have created a testing DB and experimented with each of
them separately - references were set to Outlook 11 object library and all
worked well. I thought I might be able to adapt both to create what I wanted.
After the first try with the code below I received the error message "94 -
invalid use of null" so then I checked things like 'zero length string
allowed' and played with a couple of other things and now I am getting "Error
438 - Object does not support this property or method."
This is the code I have tried:
Private Sub AddContact_Click()
On Error GoTo AddContact_Err
' Save record first to be sure required fields are filled.
DoCmd.RunCommand acCmdSaveRecord
' Exit the procedure if Contact has been added to Outlook.
If Me!AddedToOutlook = True Then
MsgBox "This Contact has already been added to Microsoft Outlook"
Exit Sub
' Add a new contact.
Else
Dim outobj As Outlook.Application
Dim outcontact As Outlook.ContactItem
Set outobj = CreateObject("outlook.application")
Set outcontact = outobj.CreateItem(olContactItem)
With outcontact
.FirstName = Me!ContactFirst
.LastName = Me!ContactLast
.JobTitle = Me!JobTitle
.CompanyName = Me!Company
.BusinessTelephoneNumber = Me!BusinessPhone
If Not IsNull(Me!BusinessFax) Then .BusinessFaxNumber =
Me!BusinesFax
If Not IsNull(Me!Mobile) Then .MobileTelephoneNumber =
Me!Mobile
If Not IsNull(Me!Email) Then .Email1Address = Me!Email
If Not IsNull(Me!Address) Then .BusinessAddress = Me!Address
If Not IsNull(Me!City) Then .BusinessAddressCity = Me!City
If Not IsNull(Me!State) Then .BusinessAddressState = Me!State
If Not IsNull(Me!Postcode) Then .BusinessAddressPostalCode =
Me!Postcode
If Not IsNull(Me!Country) Then .BusinessAddressCountry =
Me!Country
If Not IsNull(Me!Notes) Then .Notes = Me!Notes
.Save
End With
End If
' Release the Outlook object variable.
Set outobj = Nothing
' Set the AddedToOutlook flag, save the record, display a message.
Me!AddedToOutlook = True
DoCmd.RunCommand acCmdSaveRecord
MsgBox "Contact Added!"
Exit Sub
AddContact_Err:
MsgBox "Error " & Err.Number & vbCrLf & Err.Description
Exit Sub
End Sub
If anyone has some suggestions or proven code I would be extremely grateful!