Results 1 to 10 of 10
  1. #1
    5 Star Lounger kmurdock's Avatar
    Join Date
    Feb 2003
    Location
    Pacific Grove, California, USA
    Posts
    716
    Thanks
    10
    Thanked 34 Times in 28 Posts

    Contact Item vs. Distribution List Item (VBA/Word 2003 SP1)

    Aye me,

    I am struggling with code that has worked very nicely -- it populates a listbox with all the contacts I have in my Contacts folder. However, I just recently added a Distribution List to my contacts folder and the code stops when it comes across it.

    In the declarations:
    <pre>Dim oApp As Outlook.Application
    Dim oNSpc As NameSpace
    Dim oFolder As Outlook.MAPIFolder
    Dim oItms As Outlook.Items
    Dim oItm As Outlook.ContactItem</pre>

    In the sub:
    <pre>Set oApp = CreateObject("Outlook.Application")
    Set oNSpc = oApp.GetNamespace("MAPI")
    Set oFolder = oNSpc.GetDefaultFolder(olFolderContacts)
    Set oItms = oFolder.Items

    For Each oItm In oItms
    If oItm.FullName = "" Then 'if there's no contact, use company name
    AddListItem Me.lstContactList, oItm.CompanyName
    Else
    If oItm.FileAs = oItm.CompanyName Then
    AddListItem Me.lstContactList, oItm.CompanyName
    ElseIf oItm.FileAs = oItm.CompanyName & vbCr & oItm.FileAs Then
    AddListItem Me.lstContactList, oItm.CompanyAndFullName
    ElseIf oItm.FileAs = oItm.FullNameAndCompany Then
    AddListItem Me.lstContactList, oItm.FullNameAndCompany
    Else
    AddListItem Me.lstContactList, oItm.FileAs
    End If
    End If
    x = x + 1
    Next oItm
    </pre>


    The last line - "Next oItm" - is where it breaks down, when the next oItm is the Distribution List. I don't know quite how to qualify it - how can I separate the ContactItem from the non-ContactItems (a DistListItem?)?

    Thanks,
    Kim

  2. #2
    3 Star Lounger
    Join Date
    Aug 2001
    Location
    Jeddah, Saudi Arabia
    Posts
    243
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Contact Item vs. Distribution List Item (VBA/Word 2003 SP1)

    Because ContactItems and DistListItems have different properties you have to test which kind of item it is. Unfortunately there is no built-in method or property that tells you this. it would be nice to have an 'IsDistributionListItem' property of the ContactItem.

    To differentiate between them I use this function to return whether or not the item is a distribution list.

    Private Function IsDistributionListItem(pItem As Object) As Boolean
    Dim s As String

    On Error GoTo NotDistributionListItem
    s = pItem.DLName ' This property only exists for Distribution List Items

    IsDistributionListItem = True

    Exit Function

    NotDistributionListItem:

    IsDistributionListItem = False

    End Function

    I hope this helps.

    Regards,
    Kevin Bell

  3. #3
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,508
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Re: Contact Item vs. Distribution List Item (VBA/Word 2003 SP1)

    You can separate the items by examining oItm.Class; see the list of item Classes in the VBE help. That should help you with sorting the DistLisist from Contacts (or anything else that found it's way into the Contacts Folder). However, I'm not sure why your code would error out at the Next line.

    FWIW, I prefer to loop through Outlook Folder items using a counter (air code):

    For counter = 1 to folder.Items.Count
    With folder.Items(counter)
    If .Class = .olDistributionList Then
    ...
    ElseIf .Class = .olContact Then

    See if some old examples <!post=here,349321>here<!/post> and <!post=here,323204>here<!/post> are of any use. For no particular reason, most Outlook code is in the Outlook Forum.
    -John ... I float in liquid gardens
    UTC -7DS

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Contact Item vs. Distribution List Item (VBA/W

    Another code example for ferreting out items that are not ContactItems: <post#=377078>post 377078</post#>

  5. #5
    5 Star Lounger kmurdock's Avatar
    Join Date
    Feb 2003
    Location
    Pacific Grove, California, USA
    Posts
    716
    Thanks
    10
    Thanked 34 Times in 28 Posts

    Re: Contact Item vs. Distribution List Item (VBA/W

    Thank you Kevin, John and JScherr! <img src=/S/clapping.gif border=0 alt=clapping width=19 height=23>

    I made use of J's suggestion as it seemed easiest to incorporate into my existing code:
    <pre>Dim oApp As Outlook.Application
    Dim oNSpc As NameSpace
    Dim oFolder As Outlook.MAPIFolder
    Dim oItms As Outlook.Items
    Dim oItm As Object 'CHANGED DECLARATION FROM CONTACT ITEM TO OBJECT
    </pre>


    In the Sub itself:
    <pre>Set oApp = CreateObject("Outlook.Application")
    Set oNSpc = oApp.GetNamespace("MAPI")
    Set oFolder = oNSpc.GetDefaultFolder(olFolderContacts)
    Set oItms = oFolder.Items</pre>

    <pre>For Each oItm In oItms
    If TypeName(oItm) = "DistListItem" Then GoTo OnToTheNext

    If oItm.FullName = "" Then 'if there's no contact, use company name
    AddListItem Me.lstContactList, oItm.CompanyName
    Else
    If oItm.FileAs = oItm.CompanyName Then
    AddListItem Me.lstContactList, oItm.CompanyName
    ElseIf oItm.FileAs = oItm.CompanyName & vbCr & oItm.FileAs Then
    AddListItem Me.lstContactList, oItm.CompanyAndFullName
    ElseIf oItm.FileAs = oItm.FullNameAndCompany Then
    AddListItem Me.lstContactList, oItm.FullNameAndCompany
    Else
    AddListItem Me.lstContactList, oItm.FileAs
    End If
    End If
    x = x + 1
    OnToTheNext:
    Next
    </pre>


    This works wonderfully! Thank you again! <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>
    Kim

  6. #6
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Contact Item vs. Distribution List Item (VBA/W

    A completely irrelevant comment on the coding style. Many programmers think that GoTo should be avoided wherever possible. In this case you could use
    <code>
    If TypeName(oItm) <> "DistListItem" Then
    <font color=448800>' Put the rest of the code here</font color=448800>
    Endif

    </code>instead of<code>

    If TypeName(oItm) = "DistListItem" Then GoTo OnToTheNext
    <font color=448800>' Put the rest of the code here</font color=448800>
    OnToTheNext:</code>

    StuartR

  7. #7
    5 Star Lounger kmurdock's Avatar
    Join Date
    Feb 2003
    Location
    Pacific Grove, California, USA
    Posts
    716
    Thanks
    10
    Thanked 34 Times in 28 Posts

    Re: Contact Item vs. Distribution List Item (VBA/W

    Hi Stuart,

    That's an interesting bit of info that I've not heard before. Do you know why?

    Kim

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Contact Item vs. Distribution List Item (VBA/W

    Program code is much easier to read and to maintain if it uses logical "blocks" such as If ... End If, Do ... Loop, For ... Next etc. (preferably with consistent indentation). GoTo, if used indiscriminately, can lead to so-called "spaghetti" code that is virtually impossible to untangle. The only situation where GoTo is really necessary is in specifying error handling: On Error GoTo <somelabel>. Apart from that, it's best to avoid the use of GoTo as much as possible.

    There is a famous short (but hard to read) article from 1968 by Edsger Dijkstra titled Go To Statement Considered Harmful.

  9. #9
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Vienna, Wien, Austria
    Posts
    5,009
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Contact Item vs. Distribution List Item (VBA/W

    >There is a famous short (but hard to read) article from 1968 by Edsger Dijkstra

    Perhaps the text was written in "Neder-English" - and not translated from Nederlands, but, as a "multiple versions" English scholar, the following sentence seems virtually unparsable:

    "In the case that a textual index points to the interior of a procedure body the dynamic progress is only characterized when we also give to which call of the procedure we refer." [Para 6]

    The following also has it challenges:

    "Usually, people take into account as well the values of some well chosen variables, but this is out of the question because it is relative to the progress that the meaning of these values is to be understood!" [Para 10]

    OTOH a quick stroll through some of the articles in the Wikipedia can raise similar issues! <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

    Thanks for a stimulating reference. As your clear paraphrase indicates, Dijkstra's basic conclusion was: "The only situation where GoTo is really necessary is in specifying error handling".
    Gre

  10. #10
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Contact Item vs. Distribution List Item (VBA/W

    Some parts of the article show traces of being translated from the Dutch.

    In the first sentence you quote, for example, the "give to" seems to be a literal translation of the Dutch verb for "indicate" (the sentence doesn't become much clearer though <img src=/S/grin.gif border=0 alt=grin width=15 height=15>)

Posting Permissions

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