Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 26

Thread: Solved: Display Outlook Contacts in Word

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    VBAX Regular
    Joined
    May 2004
    Location
    Driffield, East Yorkshire, Egnland
    Posts
    69
    Location

    Question Solved: Display Outlook Contacts in Word

    Hi folks,

    This post was asked on another forum, I'm hoping someone here can help...

    (I have, of course, let the user know that it's posted here and given him a link...)

    Quote Originally Posted by DomFino
    I have a MS Word 2000 template that uses the function below to open the address book in word so the uses can select a name from the list that then populates several fields in the template. Unfortunately, I can only get this to work with the default address book .

    What I would prefer to do is have the Contact Folder open rather the default address book. I have searched the Internet and found a reference to:

    GetAddressList
    FolderName:=contactFolder

    However, I am not sure this is what is needed here.

    Can anyone help with identifying the correct modification to the code below so that the Application.GetAddress opens the Contact Folder?

    Thanks,
    Dom
    [vba]
    Public Sub InsertAddressFromOutlook()
    Dim strCode As String, strAddress As String
    Dim iDoubleCR As Integer

    'Set up the formatting codes in strCode
    strCode = "<PR_DISPLAY_NAME>" & vbCr & _
    "<PR_POSTAL_ADDRESS>" & vbCr & _
    "<PR_OFFICE_TELEPHONE_NUMBER>" & vbCr

    'Display the 'Select Name' dialog, which lets the user choose
    'a name from their Outlook address book
    strAddress = Application.GetAddress(AddressProperties:=strCode, _
    UseAutoText:=False, DisplaySelectDialog:=1, _
    RecentAddressesChoice:=True, UpdateRecentAddresses:=True)
    'If user cancelled out of 'Select Name' dialog, quit
    If strAddress = "" Then Exit Sub

    'Eliminate blank paragraphs by looking for two carriage returns in a row
    iDoubleCR = InStr(strAddress, vbCr & vbCr)
    Do While iDoubleCR <> 0
    strAddress = Left(strAddress, iDoubleCR - 1) & _
    Mid(strAddress, iDoubleCR + 1)
    iDoubleCR = InStr(strAddress, vbCr & vbCr)
    Loop

    'Strip off final paragraph mark
    strAddress = Left(strAddress, Len(strAddress) - 1)
    'Insert the modified address at the current insertion point

    Selection.Range.Text = strAddress

    End Sub
    [/vba]

    Cheers,

    Ad

  2. #2
    Site Admin
    The Princess
    VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    You know...Outlook is so whacky, I think we might want to get the version in here right away, Ad.
    ~Anne Troy

  3. #3
    VBAX Regular
    Joined
    May 2004
    Location
    Driffield, East Yorkshire, Egnland
    Posts
    69
    Location
    I *think* its 2000 - Dom can you confirm please? (You'll need to register lol)

    Ad

  4. #4
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    DomFino did register, Ad.
    Little over an hour ago.
    ~Anne Troy

  5. #5
    VBAX Regular
    Joined
    Jul 2004
    Location
    Sykesville, Maryland
    Posts
    53
    Location
    Thanks for everyones reply so far. Yes, I am using Word 2K and Windows 2K.

  6. #6
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    I am not sure what the problem is. I do not use Outlook, but I have it. My Contacts Folder wqs empty. I made one entry and ran the above code from Word. It opened the contacts list, and inserted the entry. name + email address.

    What is the problem?

  7. #7
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    I just made 14 more (fake) entries, and the dialog box shows them all up, the one I select is inserted into the document.

    Is it that it does not look like the Contacts Folder itself? It is the Select Name dialog, but the contents ARE the Contacts Folder contents. i do not have a Word AddressBook, I newly created entried right in the Contact Folder, and that is what shows.

    What is the problem?

  8. #8
    VBAX Regular
    Joined
    Jul 2004
    Location
    Sykesville, Maryland
    Posts
    53
    Location
    Gary,
    Thanks for your time and effort. I may not have explained the problem well enough in my original post.

    The code works fine. However, since we are on a network with many outlook folders for each department, division, etc., the following occurs.

    The user double clicks on the field that triggers the code.
    The outlook "Show Name From List" oppens to the default "Global Address List". The user must select the drop down and scroll through all the address lists until they find the "Contacts", which is at the bottom of a long list.

    However, what I wanted to do is have the user doube click on the field that triggers the code. The outlook Show Name From List" will open to the "Contacts" rather then the "Global Address List".

    Some how I need to have the GetAddressList command to set the
    FolderName:=contactFolder

    I hope this helps better explain the problem
    Thanks again
    Dom

  9. #9
    VBAX Tutor jamescol's Avatar
    Joined
    May 2004
    Location
    Charlotte, NC
    Posts
    251
    Location
    I wrote an application that needed to do something similar. During my research, I was told there is no way to display on of the default dialog boxes and for it to display a specific addresslist. The built-in dialogs display whatever addresslist the Outlook user has chosen as his default.

    To display a specific folder, you can use the Outlook View Control and set its default folder to what you want. To use this control, you need to add a UserForm, then add the appropriate controls and code.

    Interestingly, you can access the contents of a specific address list programmatically. So you could also use a listbox control and populate it with the address entries of the PAB or Contacts folder.

    If you wnt to pursue either option, let me know and I can post some code.

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

  10. #10
    VBAX Regular
    Joined
    Jul 2004
    Location
    Sykesville, Maryland
    Posts
    53
    Location
    James,
    Thanks for your reply. I understand what you are saying and have also done some additional research since my post. I found a link that shows me how to do exactly what I want. I have posted it below in case anyone else comes up with a similar need.

    http://msdn.microsoft.com/library/de...ml/ivb0076.asp

  11. #11
    VBAX Tutor jamescol's Avatar
    Joined
    May 2004
    Location
    Charlotte, NC
    Posts
    251
    Location
    I considered that article when I was developing my project. For performance reasons, I chose to use the Outlook View Control. A couple of items you might want to consider when using the code from that article:

    1. The code doesn't test for the absence of any contacts. I know several users who don't use their Contacts folder. If they were to see an empty list box, they might get confused and call the helpdesk. So consider adding a test for this condition.

    2. Since you are running OL2000, you are OK using the olFolderContacts. Keep in mind that prior to OL2000, contacts were stored in the Personal Address Book. If a user happens to still run an earlier version, the code will error.

    Good luck! Post your completed code in the KB to share with everyone.

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

  12. #12
    VBAX Regular JOrzech's Avatar
    Joined
    Jun 2004
    Location
    Upstate New York
    Posts
    83
    Location
    You may want to try:
    Change Address Book settings

    On the Tools menu, click E-mail Accounts.
    1. Under Directory, select View or change existing directories or address books, and then click Next.
    2. Click the address book you want to change, and then click Change.
    3. Make the changes you want, and then click OK.
    4. To change additional settings, click More Settings.
    5. Click OK, and then click Finish.
    This may also provide some useful information:
    http://www.slipstick.com/contacts/insword.htm
    Joanne

  13. #13
    VBAX Regular
    Joined
    Jul 2004
    Location
    Sykesville, Maryland
    Posts
    53
    Location
    Well, I went for following code to pop up a form that shows the Contacts data. It works great and is not too bad regarding response time needed to populate the listbox. I still need to figure out how to test for the absence of any contacts as suggested by James in the post above. However, the code does work and will do for now.

    Thank you all for your input, comments, and suggestions.
    Dom

    [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
    Private Sub UserForm_Initialize()
    Dim oNspc As NameSpace
    Dim oItm As ContactItem
    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")
    For Each oItm In oNspc.GetDefaultFolder _
    (olFolderContacts).Items

    With Me.cboContactList
    .AddItem (oItm.FullName)
    .Column(1, x) = oItm.BusinessAddress
    .Column(2, x) = oItm.BusinessTelephoneNumber
    End With
    x = x + 1
    Next oItm
    StatusBar = ""
    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]

  14. #14
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    Hey, Dom. Good to hear it's working *so* far. So, you want to keep this open? I assume yes? Presumably, James can help with that...

    James...if you can't, let's PM steph...
    ~Anne Troy

  15. #15
    VBAX Tutor jamescol's Avatar
    Joined
    May 2004
    Location
    Charlotte, NC
    Posts
    251
    Location
    Hey Dom,
    Glad to hear you are making progress! I modified your code to check for an empty Contacts folder and added an inner If..Then to only add Contacts to the list. If you want Distribution Lists added to your contact list, just remove the If..Then statement.
    [vba]
    Private 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

    '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

    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
    [/vba]

    Let us know if this works.

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

  16. #16
    VBAX Regular
    Joined
    Jul 2004
    Location
    Sykesville, Maryland
    Posts
    53
    Location
    Hi Dreamboat and James,

    Thanks again for the suggestions and sample code. I tried the revised code without a distribution list and it works fine. However, if I add a distribution list, I receive the following error.

    Run-Time error '13':
    Type mismatch

    I suspect the following line of code is the culpert but am not sure. Any Suggestios?

    [VBA]If oItm.Class = olContact Then[/VBA]

  17. #17
    VBAX Tutor jamescol's Avatar
    Joined
    May 2004
    Location
    Charlotte, NC
    Posts
    251
    Location
    Dom,
    I copied the code from my post and it ran without the error you mention. My guess is that there is something else, like an unitialized/undeclared variable or something.

    Try stepping through the procedure (highlight the procedure name and press F8, then F8 through every line), and find out exactly what triggers the error message.

    Alternately, copy your code here exactly as you have it written and I will look at it.

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

  18. #18
    VBAX Regular
    Joined
    Jul 2004
    Location
    Sykesville, Maryland
    Posts
    53
    Location
    James,

    Thanks for sticking with me on this problem. I really appreciate it and I am learning something new to boot.



    I am out of the office all day tomorrow but will do as you suggested first thing Wednesday and post my result.

    Thanks Again,

    Dom

  19. #19
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Dom, could you post a file with this form in it? I will include it in the Word Addin.

    Thanks.

  20. #20
    VBAX Regular
    Joined
    Jul 2004
    Location
    Sykesville, Maryland
    Posts
    53
    Location
    Gerry,
    Hope the attached comes through okay.
    Dom

Posting Permissions

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