A Microsoft Outlook email forum. Outlook Banter

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » Outlook Banter forum » Microsoft Outlook Email Newsgroups » Outlook and VBA
Site Map Home Register Authors List Search Today's Posts Mark Forums Read Web Partners

Using SenderEmailAddress



 
 
Thread Tools Search this Thread Display Modes
  #1  
Old June 18th 07, 07:14 PM posted to microsoft.public.outlook.program_vba
BrianL
external usenet poster
 
Posts: 13
Default Using SenderEmailAddress

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
  #2  
Old June 18th 07, 08:23 PM posted to microsoft.public.outlook.program_vba
Sue Mosher [MVP-Outlook]
external usenet poster
 
Posts: 11,651
Default Using SenderEmailAddress

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

--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54


"BrianL" wrote in message ...
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.

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

  #4  
Old June 18th 07, 09:16 PM posted to microsoft.public.outlook.program_vba
BrianL
external usenet poster
 
Posts: 13
Default Using SenderEmailAddress

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


  #5  
Old June 18th 07, 10:00 PM posted to microsoft.public.outlook.program_vba
BrianL
external usenet poster
 
Posts: 13
Default Using SenderEmailAddress

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?
  #6  
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.)


  #7  
Old June 18th 07, 10:54 PM posted to microsoft.public.outlook.program_vba
BrianL
external usenet poster
 
Posts: 13
Default Using SenderEmailAddress

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 [MVP - Outlook]" wrote:

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.


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

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...en-us%3b292063

--
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:

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 [MVP - Outlook]" wrote:

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.


  #9  
Old June 19th 07, 03:32 PM posted to microsoft.public.outlook.program_vba
BrianL
external usenet poster
 
Posts: 13
Default Using SenderEmailAddress

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!

"Eric Legault [MVP - Outlook]" wrote:

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...en-us%3b292063


 




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
senderemailaddress marco Outlook and VBA 1 June 7th 07 01:20 PM
SenderEmailAddress in VB.NET on Outlook 2003 [email protected] Add-ins for Outlook 1 April 10th 07 04:27 PM
How to change read only SenderName and SenderEmailAddress [email protected] Outlook - General Queries 3 April 19th 06 08:22 PM


All times are GMT +1. The time now is 12:56 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.Search Engine Friendly URLs by vBSEO 2.4.0
Copyright ©2004-2025 Outlook Banter.
The comments are property of their posters.