Consulting

Page 2 of 2 FirstFirst 1 2
Results 21 to 26 of 26

Thread: Solved: Display Outlook Contacts in Word

  1. #21
    VBAX Regular
    Joined
    Jul 2004
    Location
    Sykesville, Maryland
    Posts
    53
    Location
    Jame,
    I did as you suggested and found that the code errors on the line that reads For Each oItm In oItems

    [VBA]
    Public Sub InsertIntoDoc(All As Boolean)
    Dim itm As Variant
    With lstFieldList
    For x = 0 To .ListCount - 1
    If All Then .Selected(x) = All
    If .Selected(x) = True Then
    With Selection
    .InsertAfter (lstFieldList.List(x))
    .Collapse (wdCollapseEnd)
    .Paragraphs.Add
    End With
    End If
    Next x
    End With
    End Sub
    Sub UserForm_Initialize()
    Dim oNspc As NameSpace
    Dim oItm As ContactItem
    Dim oItems As Items


    Dim x As Integer
    If Not DisplayStatusBar Then
    DisplayStatusBar = True
    End If

    StatusBar = "Please Wait..."
    x = 0

    Set oApp = CreateObject("Outlook.Application")
    Set oNspc = oApp.GetNamespace("MAPI")
    Set oItems = oNspc.GetDefaultFolder(olFolderContacts).Items

    'Make sure the Contacts folder contains entries

    If oItems.Count > 0 Then

    For Each oItm In oItems ' Error Occurs Here

    'Make sure the oItm is a contact and not a DL
    'Remove this If...Then condition if you want to include DLs

    If oItm.Class = olContact Then
    'If oItm.Class <> olDistributionList Then
    With Me.cboContactList
    .AddItem (oItm.FullName)
    .Column(1, x) = oItm.BusinessAddress
    .Column(2, x) = oItm.BusinessTelephoneNumber
    End With
    x = x + 1


    End If
    Next oItm
    End If

    StatusBar = ""
    Set oItems = Nothing
    Set oItm = Nothing
    Set oNspc = Nothing
    Set oApp = Nothing

    End Sub
    Private Sub cboContactList_Change()
    Dim x As Integer
    With lstFieldList
    If .ListCount > 0 Then
    For x = 0 To .ListCount - 1
    .RemoveItem (0)
    Next x
    End If
    For x = 0 To cboContactList.ColumnCount - 1
    .AddItem (Me.cboContactList.Column(x))
    Next x
    End With
    End Sub

    Private Sub cmbAll_Click()
    Call InsertIntoDoc(True)
    End Sub
    Private Sub cmbDetail_Click()
    Call InsertIntoDoc(False)
    End Sub
    Private Sub cmbClose_Click()
    Unload Me
    End Sub
    [/VBA]

  2. #22
    VBAX Tutor jamescol's Avatar
    Joined
    May 2004
    Location
    Charlotte, NC
    Posts
    251
    Location
    Hi Dom,
    The code you posted looks right, and works on my computer. I think you might be missing a reference. From your VBE, choose the Tools | References menu. In the list that appears, make sure that you have the checkbox marked for "Microsoft Outlook xx.0 Object Library", where xx is the version number for your Outlook.

    This reference will make the Outlook object model visible to Word. Also, when you DIM your Outlook variables, be sure to qualify them with "Outlook" to differentiate them from Word objects. For instance:

    [vba]
    Dim oNspc As Outlook.NameSpace
    Dim oItm As Outlook.ContactItem
    Dim oItems As Outlook.Items
    [/vba]

    Otherwise, VBA assigns the default application's object to the variable. I should have caught that earlier for you.

    Let us know what happens.

    Cheers,
    James
    "All that's necessary for evil to triumph is for good men to do nothing."

  3. #23
    VBAX Regular
    Joined
    Jul 2004
    Location
    Sykesville, Maryland
    Posts
    53
    Location
    James,
    I checked the Microsoft Outlook Object Library and it is checked.
    I also changed the DIM Outlook variables to read as you suggested, however, I still get the same error.

    FYI
    If oItems.Count > 0 Then

    In the line of code above the count shows as 22 which is correct. There are 22 contacts and 1 distribution list.

    For Each oItm In oItems

    The above line of code shows as oItm=Nothing

  4. #24
    VBAX Tutor jamescol's Avatar
    Joined
    May 2004
    Location
    Charlotte, NC
    Posts
    251
    Location
    Dom,
    It must be something related to the VBA in Off2000, and I do not have that version to test.

    Try this way instead:
    [vba]
    Sub UserForm_Initialize()
    Dim oNspc As Outlook.NameSpace
    Dim oItems As Outlook.Items
    Dim i, x As Integer

    If Not DisplayStatusBar Then
    DisplayStatusBar = True
    End If

    StatusBar = "Please Wait..."
    x = 0

    Set oApp = CreateObject("Outlook.Application")
    Set oNspc = oApp.GetNamespace("MAPI")
    Set oItems = oNspc.GetDefaultFolder(olFolderContacts).Items

    'Make sure the Contacts folder contains entries

    If oItems.Count > 0 Then

    For i = 1 to oItems.Count

    'Make sure the oItems(i) is a contact and not a DL
    'Remove this If...Then condition if you want to include DLs

    If oItems(i).Class = olContact Then

    With Me.cboContactList
    .AddItem (oItems(i).FullName)
    .Column(1, x) = oItems(i).BusinessAddress
    .Column(2, x) = oItems(i).BusinessTelephoneNumber
    End With

    x = x + 1

    End If
    Next i
    End If

    StatusBar = ""
    Set oItems = Nothing
    Set oNspc = Nothing
    Set oApp = Nothing

    End Sub
    [/vba]

    Hopefully this one will work for you.

    James
    "All that's necessary for evil to triumph is for good men to do nothing."

  5. #25
    VBAX Regular
    Joined
    Jul 2004
    Location
    Sykesville, Maryland
    Posts
    53
    Location
    James,
    We have ignition

    Worked like a charm. I am not sure why your latest version did the trick, but as you said, it must be a quirk with VBA in Off2000.

    Thank you so much for your efforts. I certainly would have never figured this one out by myself.

    Dom

  6. #26
    VBAX Tutor jamescol's Avatar
    Joined
    May 2004
    Location
    Charlotte, NC
    Posts
    251
    Location
    Dom,
    Glad it's working!

    James
    "All that's necessary for evil to triumph is for good men to do nothing."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •