View Single Post
  #3  
Old June 18th 07, 08:28 PM posted to microsoft.public.outlook.program_vba
Eric Legault [MVP - Outlook]
external usenet poster
 
Posts: 830
Default Using SenderEmailAddress

First off, I assume you've created a UserForm in the Outlook VBA Editor?
I'll also assume you have other things on this form that you're using - but
if you just want to create a Contact you can write procedures in the
ThisOutlookSession module - no need to create a form.

Anyway, you're on the right track. You don't even need the GetDefaultFolder
call, as any new item you create using CreateItem will be saved in the
default folder for that item type.

The somewhat tricky part is getting a reference to the first e-mail in your
Inbox. If it is selected, you can do:

Dim myMailItem

If Application.ActiveExplorer.Selection.Count = 1 Then
If Application.ActiveExplorer.Selection(1).Class = olMail Then
Set myMailItem = Application.ActiveExplorer.Selection(1)
myContact.Email1Address = myMailItem.SenderEmailAddress
End If

Otherwise, you have to use the MAPIFolder.Items.Sort method using the
property in question that causes the message to be displayed first in the
list (probably Received Date descending?). Then you can use Set myMailItem =
Items(1) or Items(Items.Count) to retrieve the first or last message in the
collection.

--
Eric Legault - Outlook MVP, MCDBA, MCTS (SharePoint programming, etc.)
Try Picture Attachments Wizard for Outlook:
http://www.collaborativeinnovations.ca
Blog: http://blogs.officezealot.com/legault/


"BrianL" wrote:

I have been looking at different ways to retrieve email addresses from
senders' mail items, but I have not found one that I can get to work
correctly.

I am trying to open the "new contacts" form and put the sender's address of
the first email in my inbox to the email input. Does that make any sense?

So far I have this code to create a new contact:

Private Sub CommandButton1_Click()
Set myNameSpace = Application.GetNamespace("MAPI")
Set myFolder = myNameSpace.GetDefaultFolder(10)
Set myItem = Application.CreateItem(2)
myItem.Email1Address =
Unload Me
myItem.Display
End Sub

The only problem is that I don't know how to refrence (is that the right
word?) the first SenderEmailAddress in my inbox. All of the examples I found
concerning this used "myItem" and I'm sure that Outlook wont like me using
two myItem in one sub.

Where would I refrence the address anyway? In the same Private sub or in
ThisOutlookSession?

any tips would be apreciated.

Ads