954,535 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

VB6 - Look up email address from outlook address book?

Hello everyone,
is there a posibility that I can look up email addresses from outlook email address book instead of accepting email address from an inputbox or hard-coding it?
See my codes below:
Thanks.

SENDEMAIL:
 Set OutlookApp = CreateObject("Outlook.Application")
 Set OutlookMail = OutlookApp.CreateItem(0)
 
 OutlookMail.To = EmailID
 OutlookMail.Subject = "Project Status"
 OutlookMail.Body = "This is VB email test"
 
 If Len(MailAttach) = 0 Then
    OutlookMail.Attachments.Add MailAttach  '"C:\ProjectStatus.xls"
 End If


tgifgemini

tgifgemini
Junior Poster
113 posts since Jul 2007
Reputation Points: 22
Solved Threads: 0
 

Hello everyone, is there a posibility that I can look up email addresses from outlook email address book instead of accepting email address from an inputbox or hard-coding it? See my codes below: Thanks.

SENDEMAIL:
 Set OutlookApp = CreateObject("Outlook.Application")
 Set OutlookMail = OutlookApp.CreateItem(0)
 
 OutlookMail.To = EmailID
 OutlookMail.Subject = "Project Status"
 OutlookMail.Body = "This is VB email test"
 
 If Len(MailAttach) = 0 Then
    OutlookMail.Attachments.Add MailAttach  '"C:\ProjectStatus.xls"
 End If

tgifgemini



Answer =No as well as Yes.

No means not directly and Yes means indirectly.

Why not directly ? Because it exposes the address book to hackers very easily.

How indirectly ? There is a Outlook Express API (OEAPI) developed by third parties which you can use it, not only in VB but also in host of other programming languages.

If you are a very advanced programmer you would be knowing it already or else while you approach to that category you will come across with it.

Be contended now and get back to me later.

Happy programming

regards
AV Manoharan

AV Manoharan
Junior Poster
166 posts since Jun 2007
Reputation Points: 10
Solved Threads: 9
 

Hi tgif,

Add a Listbox to the form and
use this code:

Option Explicit
Dim ola As Outlook.AddressList
Dim ole As Outlook.AddressEntry
List1.Clear
Private Sub address_book_Click()
On Error Resume Next
Set ola = Application.Session.AddressLists("MyContacts") 
For Each ole In ola.AddressEntries
    List1.AddItem ole
Next
Set ola = Nothing
Set ole = Nothing
End Sub


In above code, change MyContacts to the name of ur Address Book.

Regards
Veena

QVeen72
Posting Shark
950 posts since Nov 2006
Reputation Points: 84
Solved Threads: 143
 

Hi tgif, Add a Listbox to the form and use this code:

Option Explicit
Dim ola As Outlook.AddressList
Dim ole As Outlook.AddressEntry
List1.Clear
Private Sub address_book_Click()
On Error Resume Next
Set ola = Application.Session.AddressLists("MyContacts") 
For Each ole In ola.AddressEntries
   List1.AddItem ole
Next
Set ola = Nothing
Set ole = Nothing
End Sub

In above code, change MyContacts to the name ofur Address Book. Regards Veena



Veena, It is not his addressbook he wants. He wants address book of outlook express. Please help him to find out!

AV Manoharan
Junior Poster
166 posts since Jun 2007
Reputation Points: 10
Solved Threads: 9
 

Hi tgif,

If u dont know the Address Book name, u can loop thru all the AddressBooks and List It,
Add a "ListView" Control to ur form and rename it as "lvw", Check this code:

Private Sub FillListView()

Dim olApp As Outlook.Application
Dim olNS As Outlook.NameSpace
Dim olAL As Outlook.AddressList
Dim olAE As Outlook.AddressEntry
Dim olMail As Outlook.MailItem

    Set olApp = New Outlook.Application
    Set olNS = olApp.GetNamespace("MAPI")
    For Each olAL In olNS.AddressLists
        For Each olAE In olAL.AddressEntries
            lvw.ListItems.Add , , olAE.Name
            lvw.ListItems(lvw.ListItems.Count).SubItems(1) = olAE.Address
            lvw.ListItems(lvw.ListItems.Count).SubItems(2) = olAE.ID
            lvw.ListItems(lvw.ListItems.Count).Tag = olAE.ID
        Next
    Next
End Sub


REgards
Veena

QVeen72
Posting Shark
950 posts since Nov 2006
Reputation Points: 84
Solved Threads: 143
 

Good afternoon Veena,
I am getting Run-time error 424 - Object required on this line ==>:"lvw.ListItems.Add , , OutlookAddressEntry.Name"


See my code below:

Set OutlookApp = New Outlook.Application
 Set OutlookMailItem = OutlookApp.CreateItem(0)
 Set OutlookNSpace = OutlookApp.GetNamespace("MAPI")
 
For Each OutlookAddressList In OutlookNSpace.AddressLists
    For Each OutlookAddressEntry In OutlookAddressList.AddressEntries
        lvw.ListItems.Add , , OutlookAddressEntry.Name
        lvw.ListItems(lvw.ListItems.Count).SubItems(1) = OutlookAddressEntry.Address
        lvw.ListItems(lvw.ListItems.Count).SubItems(2) = OutlookAddressEntry.ID
        lvw.ListItems(lvw.ListItems.Count).Tag = OutlookAddressEntry.ID
    Next
Next

EmailID = lvw.ListItems(lvw.ListItems.Count).SubItems(1)
 
 OutlookMailItem.To = EmailID
 OutlookMailItem.Subject = "Project Status"
 OutlookMailItem.Body = "This is VB email test"
 
 If Len(MailAttach) > 0 Then
    OutlookMailItem.Attachments.Add "C:\ProjectStatus.xls", olByValue, 1, "ProjectStatus"
 End If
 
 'OutlookMailItem.Display      'To display the email
 OutlookMailItem.Send         'To send the email


Thanks
tgifgemini

tgifgemini
Junior Poster
113 posts since Jul 2007
Reputation Points: 22
Solved Threads: 0
 

Hi tgif,

Just remove the Listview, and add a MSFlexgrid, rename it as grd. check this code:

Private Sub FillListView()
Dim olApp As Outlook.Application
Dim olNS As Outlook.NameSpace
Dim olAL As Outlook.AddressList
Dim olAE As Outlook.AddressEntry
Dim olMail As Outlook.MailItem
Dim i As Long
Grd.Rows=1
Grd.Cols=4
i=0    
Set olApp = New Outlook.Application   
Set olNS = olApp.GetNamespace("MAPI")  
For Each olAL In olNS.AddressLists  
      For Each olAE In olAL.AddressEntries
          i=i+1
         Grd.rows=i+1     
         Grd.textMatrix(i,0)=olAE.Name
         Grd.textMatrix(i,1)=olAE.Address
         Grd.textMatrix(i,2)=olAE.ID
         Grd.textMatrix(i,3)=olAE.ID        
   Next    
Next
End Sub


Regards
Veena

QVeen72
Posting Shark
950 posts since Nov 2006
Reputation Points: 84
Solved Threads: 143
 

Good morning Veena,
Your directives is getting me closer to my objective. I did as you said. Now, I am stepping thru the program and I can see the email address information being loaded into the flexgrid control. However at a point, I want to be able to select an email address from the flexgrid, place it in the "EmailID" variable which I will subsequently point to in the "OutlookMailItem.To = EmailID"

See all my code/setup below:

Private Sub Flxgrd_Click()
Dim OutlookApp As Outlook.Application
Dim OutlookNameSpace As Outlook.NameSpace
Dim OutlookAddressList As Outlook.AddressList
Dim OutlookAddressEntry As Outlook.AddressEntry
Dim OutlookMailItem As Outlook.MailItem
Dim i As Long
Dim EmailID As String
Dim OutlookAttachment As Outlook.Attachment
Dim MailAttach As String

MailAttach = "C:\ProjectStatus.xls"

Flxgrd.Rows = 1
Flxgrd.Cols = 4
i = 0

Set OutlookApp = New Outlook.Application
Set OutlookNameSpace = OutlookApp.GetNamespace("MAPI")


For Each OutlookAddressList In OutlookNameSpace.AddressLists
      For Each OutlookAddressEntry In OutlookAddressList.AddressEntries
          i = i + 1
         Flxgrd.Rows = i + 1
         Flxgrd.TextMatrix(i, 0) = OutlookAddressEntry.Name
         Flxgrd.TextMatrix(i, 1) = OutlookAddressEntry.Address
         Flxgrd.TextMatrix(i, 2) = OutlookAddressEntry.ID
         Flxgrd.TextMatrix(i, 3) = OutlookAddressEntry.ID
   Next
Next

EmailID = "gift.xtian@nyct.com"

SENDEMAIL:
 OutlookMailItem.To = EmailID
 OutlookMailItem.Subject = "Project Status"
 OutlookMailItem.Body = "This is VB email test"
 
 If Not IsMissing(MailAttach) Then
    Set OutlookAttachment = OutlookMailItem.Attachments.Add(MailAttach)
 End If
 
 'If Len(MailAttach) > 0 Then
 '   OutlookMailItem.Attachments.Add "C:\ProjectStatus.xls", olByValue, 1, "ProjectStatus"
 'End If
 
 'OutlookMailItem.Display      'To display the email
 OutlookMailItem.Send         'To send the email
    
 Set OutlookApp = Nothing
 Set OutlookMailItem = Nothing
 Set OutlookNameSpace = Nothing
 Set OutlookAddressList = Nothing
 Set OutlookAddressEntry = Nothing
 Set OutlookAttachment = Nothing
End Sub
tgifgemini
Junior Poster
113 posts since Jul 2007
Reputation Points: 22
Solved Threads: 0
 

Good morning all:
Did anyone give a crack at my problem in my last post?
Thanks.
tgifgemini.

tgifgemini
Junior Poster
113 posts since Jul 2007
Reputation Points: 22
Solved Threads: 0
 

Hi Veena,
Sorry I've been going back and forth with this issue. But I was able to bring in the "ListView" control component and my code is executing up to a point where I get a "Runtime error - 380 - Invalid Property value" on these lines of codes:

lvw.ListItems(lvw.ListItems.Count).SubItems(1) = OutlookAddressEntry.Address
        lvw.ListItems(lvw.ListItems.Count).SubItems(2) = OutlookAddressEntry.ID
        lvw.ListItems(lvw.ListItems.Count).Tag = OutlookAddressEntry.ID


Also, I want to be able to select my desired email address from the list.Below is my entire code:

Dim OutlookApp As Outlook.Application
Dim OutlookNameSpace As Outlook.NameSpace
Dim OutlookAddressList As Outlook.AddressList
Dim OutlookAddressEntry As Outlook.AddressEntry
Dim OutlookMailItem As Outlook.MailItem
Dim i As Long
Dim EmailID As String
Dim OutlookAttachment As Outlook.Attachment
Dim MailAttach As String



MailAttach = "C:\ProjectStatus.xls"


Set OutlookApp = New Outlook.Application
Set OutlookMailItem = OutlookApp.CreateItem(olMailItem)
Set OutlookNameSpace = OutlookApp.GetNamespace("MAPI")
 
For Each OutlookAddressList In OutlookNameSpace.AddressLists
    For Each OutlookAddressEntry In OutlookAddressList.AddressEntries
        lvw.ListItems.Add , , OutlookAddressEntry.Name
        lvw.ListItems(lvw.ListItems.Count).SubItems(1) = OutlookAddressEntry.Address
        lvw.ListItems(lvw.ListItems.Count).SubItems(2) = OutlookAddressEntry.ID
        lvw.ListItems(lvw.ListItems.Count).Tag = OutlookAddressEntry.ID
    Next
Next

EmailID = "????????????????????????"
 
 OutlookMailItem.To = EmailID
 OutlookMailItem.Subject = "Project Status"
 OutlookMailItem.Body = "This is VB email test"
 
 If Len(MailAttach) > 0 Then
    OutlookMailItem.Attachments.Add (MailAttach)
 End If
 
 'OutlookMailItem.Display      'To display the email
 OutlookMailItem.Send         'To send the email
End Sub

Thanks,
tgifgemini.

tgifgemini
Junior Poster
113 posts since Jul 2007
Reputation Points: 22
Solved Threads: 0
 

Hi tgif,

use this code:

Dim TItem As ListItem
If lvw.SelectedItem.text <> "" Then
  Set TItem = lvw.SelectedItem
  EMailID=TItem.SubItems(2)
Else
  MsgBox "No Item Selected From list"
End If


Regards
Veena

QVeen72
Posting Shark
950 posts since Nov 2006
Reputation Points: 84
Solved Threads: 143
 

Good morning Veena.
Thanks for your numerous contributions. I put the codes below inside my form load() module, because I want the listview control to get populated on form load then I can select and email from the list then click the sendEmail button.
However, when I tried to compile, I get Error msg: Run-Time error 380 -Invalid property value:

This line of code generates the error:

lvw.ListItems(lvw.ListItems.Count).SubItems(1) = OutlookAddressEntry.Address


Below is my form load module:

Private Sub Form_Load()

Set OutlookApp = New Outlook.Application
Set OutlookMailItem = OutlookApp.CreateItem(olMailItem)
Set OutlookNameSpace = OutlookApp.GetNamespace("MAPI")


For Each OutlookAddressList In OutlookNameSpace.AddressLists
    For Each OutlookAddressEntry In OutlookAddressList.AddressEntries
        lvw.ListItems.Add , , OutlookAddressEntry.Name
        lvw.ListItems(lvw.ListItems.Count).SubItems(1) = OutlookAddressEntry.Address
        lvw.ListItems(lvw.ListItems.Count).SubItems(2) = OutlookAddressEntry.ID
        lvw.ListItems(lvw.ListItems.Count).Tag = OutlookAddressEntry.ID
    Next
Next
End Sub

Thanks,
tgifgemini

tgifgemini
Junior Poster
113 posts since Jul 2007
Reputation Points: 22
Solved Threads: 0
 

Hi,

Just change the code:
Set lvw.ReportType = lvwReport

Dim X As ListItem

Set OutlookApp = New Outlook.Application
Set OutlookMailItem = OutlookApp.CreateItem(olMailItem)
Set OutlookNameSpace = OutlookApp.GetNamespace("MAPI")


For Each OutlookAddressList In OutlookNameSpace.AddressLists
    For Each OutlookAddressEntry In OutlookAddressList.AddressEntries
       set X= lvw.ListItems.Add( , , OutlookAddressEntry.Name)
        X.SubItems(1) = OutlookAddressEntry.Address
        X.SubItems(2) = OutlookAddressEntry.ID
        X.Tag = OutlookAddressEntry.ID
    Next
Next


Regards
Veena

QVeen72
Posting Shark
950 posts since Nov 2006
Reputation Points: 84
Solved Threads: 143
 

Good morning Veena,
how are you doing today? I really do appreciate all your assistance. I plugged in recent code you you gave me and I am getting same "RunTime error 380 - Invalid property value". I am going nuts with this Microsoft stuff that is not telling you what the actual problem is.
And I don't know where else to look.

Thanks.
tgifgemini

tgifgemini
Junior Poster
113 posts since Jul 2007
Reputation Points: 22
Solved Threads: 0
 

Good morning Veena,
how are you doing today? I really do appreciate all your assistance. I plugged in recent code you you gave me and I am getting same "RunTime error 380 - Invalid property value" on this line of code:

x.subItems(1) = OutlookAddressEntry.Address


and a compile error: "Method or Data member not found" on this line: [Set lvw.ReportType = lvwReport[/code]I am going nuts with this Microsoft stuff that is not telling you what the actual problem is.
And I don't know where else to look.

Question: Shouldn't I first Dimension "lvw" as something before the 'set lvw.ReportType = lvwReport' statement?

Thanks.
tgifgemini

tgifgemini
Junior Poster
113 posts since Jul 2007
Reputation Points: 22
Solved Threads: 0
 

Veena,
Actually, I went ahead and created ListView header columns which I didn't have initially, therefore since I want to display 3 items in the columns, it didn't find any columns to append the items.

This is what I did:

'create columns
lvw.ColumnHeaders.Clear
lvw.ColumnHeaders.Add , ,"Address"
lvw.ColumnHeaders.Add , ,"ID"


And it populated the ListView with the the physical names "gift, xtian" etc.
So I used the first subscript(1) as the address and it worked like magic.
Thanks.
You're a true trooper for holding my hand all along.
tgifgemini.

tgifgemini
Junior Poster
113 posts since Jul 2007
Reputation Points: 22
Solved Threads: 0
 

Hi,

OK, its:

lvw.view=3

And u have to add column headers..
Write this code b4 populating the list..

Dim lvwColumn As ColumnHeader

     lvwColumn = New ColumnHeader()
     lvwColumn.Text = "Name"   
     lvw.Columns.Add(lvwColumn)
 
     lvwColumn = New ColumnHeader()
     lvwColumn.Text = "Address"   
     lvw.Columns.Add(lvwColumn)

     lvwColumn = New ColumnHeader()
     lvwColumn.Text = "ID"   
     lvw.Columns.Add(lvwColumn)


Sorry for the inconvinience..

Regards
Veena

QVeen72
Posting Shark
950 posts since Nov 2006
Reputation Points: 84
Solved Threads: 143
 

Hello All.

You people are waisting your time and energy. And I can't understand, why people gives in this thread so many type of codes, telling one way or the other I am soory, please change to this, please omit that etc. without first understanding the basic of it or trying the code themselves! If you have heeded to my advice in this forum by telling gemini that the answer is NO as well as Yes.

I will put it briefly below. Think about it and answer me.

First of all, if you are thinking Outlook Express, just forget it. The only three ways of integrating with Outlook Express are (and this is just my guessing) by paying Microsoft a lot of money and by giving them a really good reason to why you want to do that, or by hooking in through the encryption-entry, or by creating a proper hook and hacking your way in there.

Could you make out any thing from the bold entries above?

Happy Programming

AV Manoharan

AV Manoharan
Junior Poster
166 posts since Jun 2007
Reputation Points: 10
Solved Threads: 9
 

Hi AV Manoharan,
I don't know what you are babbling about, but Veena have correctly answered my question, so be cool.
tgifgemini.

tgifgemini
Junior Poster
113 posts since Jul 2007
Reputation Points: 22
Solved Threads: 0
 
Hi AV Manoharan, I don't know what you are babbling about, but Veena have correctly answered my question, so be cool. tgifgemini.



Correctly answered what? Getting email address from the outlook express? The email addresses stored in the outlook express are encrypted ! and you and Veena Could hack it and decrypt it. Fantastic Dear.


regards
AV Manoharan

AV Manoharan
Junior Poster
166 posts since Jun 2007
Reputation Points: 10
Solved Threads: 9
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You