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

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/


"BrianL" wrote:

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

"Sue Mosher [MVP-Outlook]" wrote:

"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.)


Ads