View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
[email protected] Steven@pctamers.eu is offline
external usenet poster
 
Posts: 30
Default Excel export to Outlook:- Missing Microsoft Outlook 16.0 object library

Hi Garry

thanks for this. I've included the code you posted; I hope I've done it correctly (I've put all my vba below this message). It works fine on my pc but I won't get a chance to try it on the user's pc until later.

Am I understanding this right, by incorporating the code you posted my "code should ref whatever version is on the host machine." ?

Thanks again!

Steve

Public gbOutlookIsRunning As Boolean

Private Function bOutlookAvailable() As Boolean
Dim appOL As Object, bWasRunning As Boolean

' Attempt to get a reference to a currently open
' instance of Outlook.
On Error Resume Next
Set appOL = GetObject(, "Outlook.Application")
' If this fails, attempt to start a new instance.
If appOL Is Nothing Then
Set appOL = CreateObject("Outlook.Application")
Else
' Otherwise flag that Outlook was already running
' so that we don't try to close it.
bWasRunning = True
End If
On Error GoTo 0

' Return the result of the test.
If Not appOL Is Nothing Then
' If we started Outlook we need to close it.
If Not bWasRunning Then appOL.Quit
Set appOL = Nothing: bOutlookAvailable = True
Else
bOutlookAvailable = False
End If
gbOutlookIsRunning = bWasRunning
End Function 'bOutlookAvailable

Sub ExcelWorksheetDataAddToOutlookContacts1()

Application.ScreenUpdating = False

If MsgBox("Have you checked if this client has an existing record with us?", vbQuestion + vbYesNo, "MOAIFA Question") < vbYes Then

Exit Sub

End If

' Test if Outlook is running
Call bOutlookAvailable


'Automating Outlook from Excel: This example uses the Application.CreateItem Method to export data from an Excel Worksheet to the default Contacts folder.
'Automate using Early Binding: Add a reference to the Outlook Object Library in Excel (your host application) by clicking Tools-References in VBE, which will enable using Outlook's predefined constants. Once this reference is added, a new instance of Outlook application can be created by using the New keyword.

'Ensure that the worksheet data to be posted to Outlook, starts from row number 2:

'Ensure corresponding columns of data in the Worksheet, as they will be posted in the Outlook Contacts Folder:
'Column A: First Name
'Column B: Last Name
'Column C: Email Address
'Column D: Company Name
'Column E: Mobile Telephone Number

Dim applOutlook As Outlook.Application
Dim nsOutlook As Outlook.Namespace
Dim ciOutlook As Outlook.ContactItem
Dim delFolder As Outlook.folder
Dim delItems As Outlook.Items
Dim lLastRow As Long, i As Long, n As Long, c As Long

'determine last data row in the worksheet:
lLastRow = Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row

'Create a new instance of the Outlook application. Set the Application object as follows:
Set applOutlook = New Outlook.Application

'use the GetNameSpace method to instantiate (ie. create an instance) a NameSpace object variable, to access existing Outlook items. Set the NameSpace object as follows:
Set nsOutlook = applOutlook.GetNamespace("MAPI")

'----------------------------
'Empty the Deleted Items folder in Outlook so that when you quit the Outlook application you bypass the prompt: Are you sure you want to permanently delete all the items and subfolders in the "Deleted Items" folder?

'set the default Deleted Items folder:
Set delFolder = nsOutlook.GetDefaultFolder(olFolderDeletedItems)
'set the items collection:
Set delItems = delFolder.Items

'determine number of items in the collection:
c = delItems.Count
'start deleting from the last item:
For n = c To 1 Step -1
delItems(n).Delete
Next n
'----------------------------

'post each row's data on a separate contact item form:
For i = 2 To lLastRow
'Use the Application.CreateItem Method to create a new contact Outlook item in the default Contacts folder. Using this method a new contact item is always created in the default Contacts folder.
'create and display a new contact form for input:
Set ciOutlook = applOutlook.CreateItem(olContactItem)
'display the new contact item form:
ciOutlook.Display
'set properties of the new contact item:
With ciOutlook
..firstName = Sheets("Sheet1").Cells(i, 1)
..LastName = Sheets("Sheet1").Cells(i, 2)
..JobTitle = Sheets("Sheet1").Cells(i, 3)
..Email1Address = Sheets("Sheet1").Cells(i, 4)
..CompanyName = Sheets("Sheet1").Cells(i, 5)
..BusinessTelephoneNumber = Sheets("Sheet1").Cells(i, 7)
..HomeTelephoneNumber = Sheets("Sheet1").Cells(i, 6)
..MobileTelephoneNumber = Sheets("Sheet1").Cells(i, 8)
..HomeAddress = Sheets("Sheet1").Cells(i, 9)
..Body = Sheets("Sheet1").Cells(i, 10)
End With
'close the new contact item form after saving:
ciOutlook.Close olSave
Next i

'quit the Oulook application:
'applOutlook.Quit

'clear the variables:
Set applOutlook = Nothing
Set nsOutlook = Nothing
Set ciOutlook = Nothing
Set delFolder = Nothing
Set delItems = Nothing

MsgBox "Check in Outlook Contacts to make sure the record is created corretly", vbOK, "MOAIFA Question"

Application.ScreenUpdating = True
End Sub