Using SenderEmailAddress

G

Guest

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.
 
S

Sue Mosher [MVP-Outlook]

"the first SenderEmailAddress in my inbox" doesn't have any precise definition when it comes to programming Outlook. Returning the currently open or currently selected items is no problem, nor is working with all the items in a folder. But if "first" relates to what the user sees, that's not so easy (if it's possible at all) to return that specific item. Maybe you can clarify which item you're interested in.

Note that you can name your variables anything you want. If myItem is already taken, then use myItem2 or myMail or mySandwich. (Sorry, just had lunch.)
 
G

Guest

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.
 
G

Guest

After reading the two replies from you and Eric, I have decided I'll take a
different route to get the address from the email (I'll go with the one
open). Does "myMailItem" select the email highlighted or opened?

Also, I have changed the code Eric gave me to better fit what I'm atempting
to do. If you see something wrong with what I changed, let me know:

Private Sub CommandButton1_Click()
Set myNameSpace = Application.GetNamespace("MAPI")
Set myFolder = myNameSpace.GetDefaultFolder(10)
Set myItem = Application.CreateItem(2)
If Application.ActiveExplorer.Selection.Count = 1 Then
If Application.ActiveExplorer.Selection(1).Class = olMail Then
Set myMailItem = Application.ActiveExplorer.Selection(1)
myItem.Email1Address = myMailItem.SenderEmailAddress
End If
End If
Unload Me
myItem.Display
End Sub

If I am correct, this code will set the Email of the selected message as
Email1address of myItem. Then it will close the form that I had open and open
the new contact form with the Email feild set as the Sender's email address.

Is this even close to doing what I thought? The only problem is that I do
not know of a way to have outlook automatically select messages when they are
recieved. Maybe if I flag them all as a color, and then tell my script to
open messages of a certain flag color? Oh, and I am interested in the
messages that are newly recieved, and have not yet been read.

If the messages are sorted by date, then could I use "myMailItem =
Items(1)" to select the newest message? Wow, this was a lengthy response,
sorry.

~Brian
 
G

Guest

Okay, I have run into a problem. I have decided to use the SenderEmailAddress
from the newest email. The only problem is that no matter how I sort the
items in the inbox, it always grabs the SenderEmailAddress from oldest email
in my inbox.

I tried this code with both false and true and got the same result:

Set myFolder2 = myNameSpace.GetDefaultFolder(olFolderInbox)
myFolder2.Items.Sort "[ReceivedTime]", False

I'm sure that it is pointing to the right folder but I have no idea why it
wont sort it. Maybe I forgot a step?
 
G

Guest

Your code should work - good job!

Note that setting myMailItem doesn't select anything - you are merely
getting a handle to the object. There is also on way with Outlook
programming to select anything - that's always up to the user.

If you want to trap e-mails as they arrive, check out Item_Add in VBA help.
This is an event for an Items collection that you get for a specific
MAPIFolder. So if you want to trap e-mails that come into your Inbox, get
that MAPIFolder object, then get the Items collection, then set a module
level variable using the With Events keyword for that Items collection. Ping
me if you want to do it this way and need more help.

Otherwise, to get a handle to e-mails that already exist - and not worrying
about trapping them as they arrive - use Items.Sort("[ReceivedTime]", True).
That *should* put the newest item at the first position within the collection.

As for getting messages by certain properties, such as Unread or
FlagRequest, use Items.Restrict("[PROPERTYNAME] = 'PROPERTY VALUE'") to
filter 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/
 
G

Guest

when I used Items.Sort("[ReceivedTime]", True), I got a compile error. It
turns out that my Outlook gets mad when I use the ( )'s. aslo, I could not
find Item_add in any microsoft support files. hmm, does Eric have his own
coding language? I have 2003 so I can't see why there would be differences in
our Outlooks.

I havn't ever used a Module but I imagine I could learn it in 5 days or less
like I did forms and Outlooksessions. Trapping them would be a good thing for
me though. How would that work? I'll start researching Modules and how to
code them while I await your responce.

Thanks

~Brian
 
G

Guest

Sorry, use this:

Items.Sort "[ReceivedTime]", True

It's ItemAdd - my mistake.

The ThisOutlookSession module is a "built-in" module. Here's a great
article that shows how to use module event variables when trapping new mail
(it'll work for 2003):

How to create a custom rule using Visual Basic for Applications (VBA) in
Outlook 2002:
http://support.microsoft.com/default.aspx?scid=kb;en-us;292063

--
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 said:
when I used Items.Sort("[ReceivedTime]", True), I got a compile error. It
turns out that my Outlook gets mad when I use the ( )'s. aslo, I could not
find Item_add in any microsoft support files. hmm, does Eric have his own
coding language? I have 2003 so I can't see why there would be differences in
our Outlooks.

I havn't ever used a Module but I imagine I could learn it in 5 days or less
like I did forms and Outlooksessions. Trapping them would be a good thing for
me though. How would that work? I'll start researching Modules and how to
code them while I await your responce.

Thanks

~Brian

Eric Legault said:
Your code should work - good job!

Note that setting myMailItem doesn't select anything - you are merely
getting a handle to the object. There is also on way with Outlook
programming to select anything - that's always up to the user.

If you want to trap e-mails as they arrive, check out Item_Add in VBA help.
This is an event for an Items collection that you get for a specific
MAPIFolder. So if you want to trap e-mails that come into your Inbox, get
that MAPIFolder object, then get the Items collection, then set a module
level variable using the With Events keyword for that Items collection. Ping
me if you want to do it this way and need more help.

Otherwise, to get a handle to e-mails that already exist - and not worrying
about trapping them as they arrive - use Items.Sort("[ReceivedTime]", True).
That *should* put the newest item at the first position within the collection.

As for getting messages by certain properties, such as Unread or
FlagRequest, use Items.Restrict("[PROPERTYNAME] = 'PROPERTY VALUE'") to
filter the collection.
 
G

Guest

Since Items.Sort never worked for me, I used Items.Getlast to get the newest
item since it goes from oldest to newest for whatever reason. I got my code
to work for what I intended it to do so my co-worker and I are happy =D.

Thanks for all your help!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top