Solved

Search and find .net

Posted on 2011-09-13
20
254 Views
Last Modified: 2012-05-12
Hello again,

I'm now working on this code that if i select a value in my combobox, that is linked to all my Microsoft Outlook contact list, that when i click on the search button, it will put all the rest of the contact detail from outlook in deferent textbox.

But now, i don't have any error but the textbox always stay NULL.



Can you help me please?

Thanks again

 Dim oApp As Outlook.Application
        oApp = CreateObject("Outlook.Application")

        Dim oNs As Outlook.NameSpace
        oNs = oApp.GetNamespace("MAPI")
        oNs.Logon()

        Dim oItem As Outlook.ContactItem
        oItem = oApp.CreateItem(OlItemType.olContactItem)

        Me.address.Visible = True
        Me.ville.Visible = True
        Me.code_postal.Visible = True
        Me.telephone.Visible = True
        Me.courriel.Visible = True
        Me.cellulaire.Visible = True


        If Me.client.Text = oItem.FullName Then

            'Home City
            Me.ville.Text = oItem.HomeAddressCity

            'Home Postal code
            Me.code_postal.Text = oItem.HomeAddressPostalCode

            'Home Téléphone
            Me.telephone.Text = oItem.HomeTelephoneNumber

            'Home cell
            Me.cellulaire.Text = oItem.MobileTelephoneNumber

            'Email address
            Me.courriel.Text = oItem.Email1Address

        End If

Open in new window

0
Comment
Question by:Wilder1626
  • 11
  • 8
20 Comments
 
LVL 40
ID: 36532911
CreateItem creates a new contact. There is nothing in it.

There is a way to retrieve a contact, but unfortunately, its been too long since I used Outlook and I do not have it on the computer I am using this week, so I cannot remember or look for which one. I kind of remember that you need to open a folder on the namespace (oNs) with a GetFolder method or something of the like. Then, you can retrieve a list of contacts or a specific contact.

Good luck.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36532918
>  If Me.client.Text = oItem.FullName Then
I think the above condition is not true. Step through the code to find out.
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 36533033
Hello James Burger and CodeCruiser,

Ok, i'm still searching and i i found something, i will let you know.

I'm not sure if i have to change this:oItem = oApp.CreateItem(OlItemType.olContactItem)

to:
oItem = oApp.AdvancedSearch(OlItemType.olContactItem) or
oItem = oApp.GetObjectReference(OlItemType.olContactItem)


0
 
LVL 11

Author Comment

by:Wilder1626
ID: 36533120
This is my second try but still the same result:

  Dim oApp As Outlook.Application = New Outlook.Application()

        ' Get NameSpace and Logon.
        Dim oNS As Outlook.NameSpace = oApp.GetNamespace("mapi")
        oNS.Logon("Outlook", Missing.Value, False, True) ' TODO:

        ' Get the first contact from the Contacts folder.
        Dim cContacts As Outlook.MAPIFolder = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts)
        Dim oItems As Outlook.Items = cContacts.Items

        Me.address.Visible = True
        Me.ville.Visible = True
        Me.code_postal.Visible = True
        Me.telephone.Visible = True
        Me.courriel.Visible = True
        Me.cellulaire.Visible = True

        If cContacts.AddressBookName = Me.client.Text Then


            'Home City
            Me.ville.Text = oItems.HomeAddressCity

            'Home Postal code
            Me.code_postal.Text = oItems.HomeAddressPostalCode

            'Home Téléphone
            Me.telephone.Text = oItems.HomeTelephoneNumber

            'Home cell
            Me.cellulaire.Text = oItems.MobileTelephoneNumber

            'Email address
            Me.courriel.Text = oItems.Email1Address

        End If

Open in new window


Still searching
0
 
LVL 40
ID: 36533640
Slowly, but surely. You are almost there I think from my memory of old code.

Your not making it easy for you as an introduction to VB.NET. You are not working with something easy. Bridging the .NET and the COM universes is already something harder than plain .NET code. And Outlook is one of the most difficult piece of software to control through code, because its object model does not follow all the standard conventions. Even in VBA it is hard to get the grasp of it.

One convention it follows however, is that when you have a property that has a plural name, specially when it is called Items, it is a collection. olItems does not contains 1 contact, but all of them.

If you try oItems.Count, you will probably get the number of contacts in your copy of Outlook.

That means that to get one of those, you need to uses oItems(1), oItems(2), oItems(3)....

Your code would then look someting like this:
Me.ville.Text = oItems(1).HomeAddressCity
Me.code_postal.Text = oItems(1).HomeAddressPostalCode
...

Open in new window

To get another contact, change the index.

To find one, you can always look through the collection, but that could be long if you have a lot of contacts. From memory, there was a Filter property that you could set to get a specific entry, setting the filter with a String similar to a WHERE clause in SQL. Memory not very good however when I do not have IntelliSense in front of me.
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 36538691
Hello again.

I have tried to use the (1) but still have the same result. All textbox stay empty but no errors

Dim oApp As Outlook.Application = New Outlook.Application()

        ' Get NameSpace and Logon.
        Dim oNS As Outlook.NameSpace = oApp.GetNamespace("mapi")
        oNS.Logon("Outlook", Missing.Value, False, True) ' TODO:

        ' Get the first contact from the Contacts folder.
        Dim cContacts As Outlook.MAPIFolder = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts)
        Dim oItems As Outlook.Items = cContacts.Items


        Me.address.Visible = True
        Me.ville.Visible = True
        Me.code_postal.Visible = True
        Me.telephone.Visible = True
        Me.courriel.Visible = True
        Me.cellulaire.Visible = True

        If cContacts.AddressBookName = Me.client.Text Then


            'Home City
            Me.ville.Text = oItems(1).HomeAddressCity

            'Home Postal code
            Me.code_postal.Text = oItems(1).HomeAddressPostalCode

            'Home Téléphone
            Me.telephone.Text = oItems(1).HomeTelephoneNumber

            'Home cell
            Me.cellulaire.Text = oItems(1).MobileTelephoneNumber

            'Email address
            Me.courriel.Text = oItems(1).Email1Address

        End If

Open in new window


If i use the oItems.Count, i have 3 result, exactly what i have in my contact list.

I have found a vb.net and c# but i can't see how i can do the same thing in vb.net.

I'm still searching.
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 36539263
I may have an idea but i don't know if it will work.

This is my code to populate my combobox with all the contact fullname.

 ' Create Outlook application.
        Dim oApp As Outlook.Application = New Outlook.Application()

        ' Get NameSpace and Logon.
        Dim oNS As Outlook.NameSpace = oApp.GetNamespace("mapi")
        oNS.Logon("Outlook", Missing.Value, False, True) ' TODO:

        ' Get the first contact from the Contacts folder.
        Dim cContacts As Outlook.MAPIFolder = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts)
        Dim oItems As Outlook.Items = cContacts.Items

        Dim oCt As Outlook.ContactItem
        For Each oCt In oItems
            client.Items.Add(oCt.FullName)
        Next

        Try
            oCt = oItems.GetFirst()


            ' Display some common properties.
            Console.WriteLine(oCt.FullName)
            Console.WriteLine(oCt.Title)
            Console.WriteLine(oCt.Birthday)
            Console.WriteLine(oCt.CompanyName)
            Console.WriteLine(oCt.Department)
            Console.WriteLine(oCt.Body)
            Console.WriteLine(oCt.FileAs)
            Console.WriteLine(oCt.Email1Address)
            Console.WriteLine(oCt.BusinessHomePage)
            Console.WriteLine(oCt.MailingAddress)
            Console.WriteLine(oCt.BusinessAddress)
            Console.WriteLine(oCt.OfficeLocation)
            Console.WriteLine(oCt.Subject)
            Console.WriteLine(oCt.JobTitle)

Open in new window


I'm wondering if i use a listBox, is there a way to have multiple column in my list box, to be able to see all other fields beside the name:
  ' Display some common properties.
            Console.WriteLine(oCt.FullName)
            Console.WriteLine(oCt.Title)
            Console.WriteLine(oCt.Birthday)
            Console.WriteLine(oCt.CompanyName)
            Console.WriteLine(oCt.Department)
            Console.WriteLine(oCt.Body)
            Console.WriteLine(oCt.FileAs)
            Console.WriteLine(oCt.Email1Address)
            Console.WriteLine(oCt.BusinessHomePage)
            Console.WriteLine(oCt.MailingAddress)
            Console.WriteLine(oCt.BusinessAddress)
            Console.WriteLine(oCt.OfficeLocation)
            Console.WriteLine(oCt.Subject)
            Console.WriteLine(oCt.JobTitle)

So that when i select a name, it would transfer from the listbox to each textbox?

Maybe it does not make sence. I'm not to sure.
0
 
LVL 40
ID: 36539594
Did I miss something? Does it work in the new code you sent at the end of the afternoon?

-----

The original code works flawlessly for me. A simple Cut and Past from your own code.

Look at the display of the Watch windows at the bottom of the included screen capture. Empty entries return Empty strings, but if there is something, it shows.

So without being in your environement, I really do not know what is wrong. If you have the right Count, that means that you have the data.

-----

Are you sure that there is something in the fields you are referencing. Stupid to ask, but this is the kind of simple thing that arises often. This is also the type of problem that usually takes the most time to debug :-).

Some fields, such as HomeAddressCity are not always filled in, even if an address shows up in the Outlook contact form. If Outlook has not been able to split the TextBox into its components, it saves the address as it was typed and does not enter anything in the individual fields.

-----

One idea, out of the blues. I have seen that before in older versions of Office. Since I work with the English versions, I cannot test it. Try using the French equivalent of the property (oCt.Nom, oCt.Prénom, oct.Prenom). Don't be surprised if does not work, but I would give it a try.

-----

The only thing that could play the role of a 2 columns ComboBox or ListBox is the DataGridView, not an easy object to use. No multi-columns ComboBox "à la Access".

If you want to display the contents of 2 fields in a ComboBox, you have to concatenate them.

With a .NET collection, you can hide the whole collection behind the ComboBox and select the field to Display. When the user makes a selection, you can get to the SelectedItem and get the complete object. With your situation, if a user selected a name, SelectedItem would contain all the other properties of the selected contact. Unfortunately, that thing does not work with collections that come from COM.
Image1.jpg
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 36539759
This is probably the issue that i have then.

Look at my result


whatch-1.jpg
0
 
LVL 40
ID: 36539826
I know that you like to learn. You will learn something else tonight.

The Watch window does not display values automatically. Its use is to enable you to examine the value of specific variables everytime you pause the application.

For a value to appear there, you need to either drag it into the window from your code, or type it (variable, object.property, anything that produces a value) in the first column. Then you get the value in the second column.

Some values will be displayed automatically. Others, such as the one for the HomeAddressCity in my screen, forces you to click on the little icon at the right border of the second column to retrieve the value.

Type oItems(1).FirstName in the first column, you will get its value in the second one.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 11

Author Comment

by:Wilder1626
ID: 36539945
So it really search. Now i see things.

This is so great :-)


So i would say that the if statment is probably the problem then: If cContacts.AddressBookName = Me.client.Text Then

whatch-1-no-2.jpg
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 36540003
Does this tell's you something?

I just remembered that Me.client should be equal to FullName field also.
error-if.jpg
0
 
LVL 40
ID: 36540014
Almost there.

Yes, looks like what you have in your TextBox is not formatted the same way as it is in the AddressBookName.

I did not focus my attention to that part up to now, because I was on the impression that you did not get anything from Outlook.

But now that you attract my attention to your If, I see a flaw in the ideal.

AddressBookName, as well as a lot of other fields, can be formatted in many different ways. Something such as If cContacts.AddressBookName = Me.client.Text is almost sure to work very badly.

Look into your own address book and you will see that there can be a lot of variations in the format, Here are just a few:

Jacques Bourgeois
Rive Sud Pontiac Buick
Richard Sirois[Sirois.Richard@Uqam.ca] 'This is not a real name

The last one was registered as I show it to you when, while reading email, I asked Outlook to add the sender to my contacts.

In order to user your If, the user would need to type the text exactly as it appears in the address book. But the user cannot know before hand.

The only way to make sure that you can find someone that is in the list of contact is to display the list of contacts in a ComboBox (but you had this idea before, didn't you?) and get the text from there when you do your seach.
0
 
LVL 40
ID: 36540025
Same discussion for FullName. Not as problematic because there are less variations in a fullname than in an address book name, but still, a ComboBox would be the best way.
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 36540026
Oh wow, i think i have found something here.

I almost got it.

Nowing that Susan name is the 3rd name, i have put this code:


  'If oItems.AddressBookName = Me.client.Text Then
        If Me.client.Text = oItems(3).FullName Then


            'Home City
            Me.ville.Text = oItems(3).HomeAddressCity

            'Home Postal code
            Me.code_postal.Text = oItems(3).HomeAddressPostalCode

            'Home Téléphone
            Me.telephone.Text = oItems(3).HomeTelephoneNumber

            'Home cell
            Me.cellulaire.Text = oItems(3).MobileTelephoneNumber

            'Email address
            Me.courriel.Text = oItems(3).Email1Address

        Else

        End If

Open in new window


And the result is positive in snap shot bellow.

But now, i dont want to force the oItems(3).FullName. I want him to search for it.

Still searching......

You give me lots of insperarttion with the watch option now. :)
result-on-find-code.jpg
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 36540043
I'm seing that even if i use another name, it will always give me the same result in all the other textbox.

Hummmmmm
0
 
LVL 40
ID: 36540063
I wonder how you could work without knowing about the Watch window. This is the most basic tool for debugging. Note that while debugging, you can also change the value of most variables simply by typing a new value in the second column.

While I am there, I want to come back to something I said before. I don't know if it is new in 2010, I had not seen it before, but I do not use ListBoxes very often. I am working on one of my own applications right now and just saw that there is now a MultiColumn property on the ListBox. This is still missing in the ComboBox.

For your "Hummmmm" problem, you left the 3 as in index in your code. So you are always dealing with the same third contact. Time to rest a little bit maybe? :-)
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 36540084
I was just happy for a few minutes that i was able to have some results with the code.

So that means that i may be very close for the final rsult.

I hope !!!! :)

But if i remove all index (3), now, it cant find anything.


For the Listbox, are you saying that i could see all contact details in columns?

error-for-full-list.jpg
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
ID: 36540105
There is (was a few years ago) a Search or Find command somewhere in the Outlook object model. It is not easy to work with however, you need to know the SQL syntax for a WHERE clause.

Since you already have the contacts, I would simply loop throught them to get the one I am looking for:
For x As Integer = 1 To Dim oItems.Count
   If If Me.client.Text = oItems(x).FullName Then
      'Your code here, x contains the magic number
      Exit For
   End If
Next

Open in new window

0
 
LVL 11

Author Closing Comment

by:Wilder1626
ID: 36540117
WoW, we made it.

Because of you, i was able to fix this.

Thank you so much, again and i will keep this code very very close. :)

Now it's dodo time for me. My brain is spinning like crazy.

Here is the full code:


    'If oItems.AddressBookName = Me.client.Text Then
        For x As Integer = 1 To oItems.Count
            If Me.client.Text = oItems(x).FullName Then


                'Home City
                Me.ville.Text = oItems(x).HomeAddressCity

                'Home Postal code
                Me.code_postal.Text = oItems(x).HomeAddressPostalCode

                'Home Téléphone
                Me.telephone.Text = oItems(x).HomeTelephoneNumber

                'Home cell
                Me.cellulaire.Text = oItems(x).MobileTelephoneNumber

                'Email address
                Me.courriel.Text = oItems(x).Email1Address

                Exit For
            End If
        Next
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now