J
Jesse Aviles
I'm trying to send information from my mdb to Outlook (Access and Outlook
XP). I created a command button and added the following code:
Private Sub CreateOutlookContact_Click()
'Create Outlook contact based on the current information on the
Client/Vendor Form
On Error GoTo ErrorHandler
DoCmd.Hourglass True 'Changes the pointer to an hourglass as an indicator
that the sub is running
Dim objOutlook As Outlook.Application
Dim objOutlookContact As Outlook.ContactItem
Dim varFirstName As Variant, varMiddleName As Variant, varLastName As
Variant
Dim varCompany As Variant, varPosition As Variant, varMailingAddress As
Variant
Dim varTelephone As Variant, varFax As Variant, varOtherPhone As Variant
Dim varWebPage As Variant, varEmail As Variant
'Create the variables that will hold the values from the Client/Vendor form
varFirstName = Me.FirstName.Value
varMiddleName = Me.MiddleName.Value
varLastName = Me.LastName.Value
varPosition = Me.Position.Value
varCompany = Me.Company.Value
varMailingAddress = Me.MailingAddress.Value
varTelephone = Me.Telephone.Value
varFax = Me.Fax.Value
varOtherPhone = Me.OtherPhone.Value
varEmail = Me.Email.Value
varWebPage = Me.webpage.Value
'Create the Outlook session
Set objOutlook = CreateObject("Outlook.Application")
'Create the Contact Item
Set objOutlookContact = objOutlook.CreateItem(olContactItem)
'Add the current contact information to the Oulook Contact Item
With objOutlookContact
.FirstName = varFirstName
.MiddleName = varMiddleName
.LastName = varLastName
.CompanyName = varCompany
.Profession = varPosition
.MailingAddress = varMailingAddress
.BusinessTelephoneNumber = varTelephone
.BusinessFaxNumber = varFax
.OtherTelephoneNumber = varOtherPhone
.Email1Address = varEmail
.webpage = varWebPage
End With
MsgBox "Transfer Complete", , "Send Contact to Outlook"
ExitHandler:
DoCmd.Hourglass False
Set objOutlookContact = Nothing
Set objOutlook = Nothing
Exit Sub
ErrorHandler:
MsgBox "Error Number: " & Err.Number & vbCrLf & Err.Description, ,
"Error creating Oulook Contact"
GoTo ExitHandler
End Sub
If there are empty fields in the mdb (null values) I get error 94 Invalid
use of null. So I changed the With block for the following
'With objOutlookContact
If Not IsNull(varFirstName) Then objOutlookContact.FirstName =
varFirstName
If Not IsNull(varMiddleName) Then objOutlookContact.MiddleName =
varMiddleName
If Not IsNull(varLastNameName) Then objOutlookContact.LastName =
varLastName
If Not IsNull(varCompany) Then objOutlookContact.CompanyName =
varCompany
If Not IsNull(varPosition) Then objOutlookContact.Profession =
varPosition
If Not IsNull(varMailingAdress) Then
objOutlookContact.MailingAddress = varMailingAddress
If Not IsNull(varTelephone) Then
objOutlookContact.BusinessTelephoneNumber = varTelephone
If Not IsNull(varFax) Then objOutlookContact.BusinessFaxNumber =
varFax
If Not IsNull(varOtherPhone) Then
objOutlookContact.OtherTelephoneNumber = varOtherPhone
If Not IsNull(varEmail) Then objOutlookContact.Email1Address =
varEmail
If Not IsNull(varWebPage) Then objOutlookContact.webpage =
varWebPage
'End With
The code appears to run fine but the contact is not made. Thanks for your
help.
XP). I created a command button and added the following code:
Private Sub CreateOutlookContact_Click()
'Create Outlook contact based on the current information on the
Client/Vendor Form
On Error GoTo ErrorHandler
DoCmd.Hourglass True 'Changes the pointer to an hourglass as an indicator
that the sub is running
Dim objOutlook As Outlook.Application
Dim objOutlookContact As Outlook.ContactItem
Dim varFirstName As Variant, varMiddleName As Variant, varLastName As
Variant
Dim varCompany As Variant, varPosition As Variant, varMailingAddress As
Variant
Dim varTelephone As Variant, varFax As Variant, varOtherPhone As Variant
Dim varWebPage As Variant, varEmail As Variant
'Create the variables that will hold the values from the Client/Vendor form
varFirstName = Me.FirstName.Value
varMiddleName = Me.MiddleName.Value
varLastName = Me.LastName.Value
varPosition = Me.Position.Value
varCompany = Me.Company.Value
varMailingAddress = Me.MailingAddress.Value
varTelephone = Me.Telephone.Value
varFax = Me.Fax.Value
varOtherPhone = Me.OtherPhone.Value
varEmail = Me.Email.Value
varWebPage = Me.webpage.Value
'Create the Outlook session
Set objOutlook = CreateObject("Outlook.Application")
'Create the Contact Item
Set objOutlookContact = objOutlook.CreateItem(olContactItem)
'Add the current contact information to the Oulook Contact Item
With objOutlookContact
.FirstName = varFirstName
.MiddleName = varMiddleName
.LastName = varLastName
.CompanyName = varCompany
.Profession = varPosition
.MailingAddress = varMailingAddress
.BusinessTelephoneNumber = varTelephone
.BusinessFaxNumber = varFax
.OtherTelephoneNumber = varOtherPhone
.Email1Address = varEmail
.webpage = varWebPage
End With
MsgBox "Transfer Complete", , "Send Contact to Outlook"
ExitHandler:
DoCmd.Hourglass False
Set objOutlookContact = Nothing
Set objOutlook = Nothing
Exit Sub
ErrorHandler:
MsgBox "Error Number: " & Err.Number & vbCrLf & Err.Description, ,
"Error creating Oulook Contact"
GoTo ExitHandler
End Sub
If there are empty fields in the mdb (null values) I get error 94 Invalid
use of null. So I changed the With block for the following
'With objOutlookContact
If Not IsNull(varFirstName) Then objOutlookContact.FirstName =
varFirstName
If Not IsNull(varMiddleName) Then objOutlookContact.MiddleName =
varMiddleName
If Not IsNull(varLastNameName) Then objOutlookContact.LastName =
varLastName
If Not IsNull(varCompany) Then objOutlookContact.CompanyName =
varCompany
If Not IsNull(varPosition) Then objOutlookContact.Profession =
varPosition
If Not IsNull(varMailingAdress) Then
objOutlookContact.MailingAddress = varMailingAddress
If Not IsNull(varTelephone) Then
objOutlookContact.BusinessTelephoneNumber = varTelephone
If Not IsNull(varFax) Then objOutlookContact.BusinessFaxNumber =
varFax
If Not IsNull(varOtherPhone) Then
objOutlookContact.OtherTelephoneNumber = varOtherPhone
If Not IsNull(varEmail) Then objOutlookContact.Email1Address =
varEmail
If Not IsNull(varWebPage) Then objOutlookContact.webpage =
varWebPage
'End With
The code appears to run fine but the contact is not made. Thanks for your
help.