Wilder1626
asked on
Search and find .net
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
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
> If Me.client.Text = oItem.FullName Then
I think the above condition is not true. Step through the code to find out.
I think the above condition is not true. Step through the code to find out.
ASKER
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 .olContact Item)
to:
oItem = oApp.AdvancedSearch(OlItem Type.olCon tactItem) or
oItem = oApp.GetObjectReference(Ol ItemType.o lContactIt em)
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
to:
oItem = oApp.AdvancedSearch(OlItem
oItem = oApp.GetObjectReference(Ol
ASKER
This is my second try but still the same result:
Still searching
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
Still searching
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:
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.
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
...
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.
ASKER
Hello again.
I have tried to use the (1) but still have the same result. All textbox stay empty but no errors
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.
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
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.
ASKER
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.
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.Full Name)
Console.WriteLine(oCt.Titl e)
Console.WriteLine(oCt.Birt hday)
Console.WriteLine(oCt.Comp anyName)
Console.WriteLine(oCt.Depa rtment)
Console.WriteLine(oCt.Body )
Console.WriteLine(oCt.File As)
Console.WriteLine(oCt.Emai l1Address)
Console.WriteLine(oCt.Busi nessHomePa ge)
Console.WriteLine(oCt.Mail ingAddress )
Console.WriteLine(oCt.Busi nessAddres s)
Console.WriteLine(oCt.Offi ceLocation )
Console.WriteLine(oCt.Subj ect)
Console.WriteLine(oCt.JobT itle)
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.
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)
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.Full
Console.WriteLine(oCt.Titl
Console.WriteLine(oCt.Birt
Console.WriteLine(oCt.Comp
Console.WriteLine(oCt.Depa
Console.WriteLine(oCt.Body
Console.WriteLine(oCt.File
Console.WriteLine(oCt.Emai
Console.WriteLine(oCt.Busi
Console.WriteLine(oCt.Mail
Console.WriteLine(oCt.Busi
Console.WriteLine(oCt.Offi
Console.WriteLine(oCt.Subj
Console.WriteLine(oCt.JobT
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.
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
-----
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
ASKER
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.
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.
ASKER
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
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
ASKER
Does this tell's you something?
I just remembered that Me.client should be equal to FullName field also.
error-if.jpg
I just remembered that Me.client should be equal to FullName field also.
error-if.jpg
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.
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
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.
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.
ASKER
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:
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
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
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
ASKER
I'm seing that even if i use another name, it will always give me the same result in all the other textbox.
Hummmmmm
Hummmmmm
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? :-)
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? :-)
ASKER
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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).HomeAddressPosta lCode
'Home Téléphone
Me.telephone.Text = oItems(x).HomeTelephoneNum ber
'Home cell
Me.cellulaire.Text = oItems(x).MobileTelephoneN umber
'Email address
Me.courriel.Text = oItems(x).Email1Address
Exit For
End If
Next
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).HomeAddressPosta
'Home Téléphone
Me.telephone.Text = oItems(x).HomeTelephoneNum
'Home cell
Me.cellulaire.Text = oItems(x).MobileTelephoneN
'Email address
Me.courriel.Text = oItems(x).Email1Address
Exit For
End If
Next
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.