Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

capture sender's email address

Status
Not open for further replies.

nelco

Programmer
Apr 4, 2006
93
US
I have created a macro that will send a message to the sender when macro is run in outlook.

My problem is how to capture sender's email address. My current code is shown below where I have hard coded the mail id in to field. I want to capture the sender address here. I will appreciate any help..

Public Sub Reply()

Dim otlApp As New Outlook.Application
Dim myMailItem As Outlook.MailItem

Set otlNewMail = otlApp.CreateItem(olMailItem)



With otlNewMail
.To = "xyz@comcast" 'I need to capture sender's mail id here
.CC = "abc@comcast"
.Subject = "Received"
.Body = "Received your application. Thank you for applying for our grant funds. Notice of acceptance for funding applications will be communicated to you in April, 2008. Any questions, please contact our office at 112-223-3334. Keep this e-mail as a record of our office having receipt of your grant application."




End With

otlNewMail.Send



Set otlNewMail = Nothing
Set otlApp = Nothing


End Sub
 

Isn't that a recipient’s e-mail address?

Why not:
Code:
Public Sub Reply()
    
    Dim otlApp              As New Outlook.Application
    Dim myMailItem          As Outlook.MailItem
    [blue]Dim strRecEMail         As String[/blue]

    Set otlNewMail = otlApp.CreateItem(olMailItem)

    With otlNewMail
        
        .To = "xyz@comcast" 
        [blue]strRecEMail = .To[/blue]
        .CC = "abc@comcast"
        .Subject = "Received"
        .Body = "Received your application.  Thank you for applying for our grant funds.  Notice of acceptance for funding applications will be communicated to you in April, 2008.  Any questions, please contact our office at 112-223-3334.  Keep this e-mail as a record of our office having receipt of your grant application."

    End With
   
    otlNewMail.Send

Have fun.

---- Andy
 
Thanks for quick reply. I do not want to hard code the email id in to field.

When user receives mail and runs macro, it should capture the mail id from the received mail and add to the to field.

Any help?
 
With the recivied item : Item.SenderName

However if using the reply method this should be automaticly filled with the address of the sender.

Everybody body is somebodys Nutter.
 
Thanks. Reply method fills the address of the sender when I am hard coding the email of the sender..
This works good.
My problem is when I receive the grant application, I click reply and then click the macro button. This should send the template mail to sender. In my code

With otlNewMail

.To = (What code I need to write here. I do not want to hard code the mail id here)

 
Nelco,

You could call the macro from the recived mail item and use somthing like:
Code:
ReplyItem.To = Item.SenderName

otherwise you somehow need to set a reference to the recived mail in order to pick out the sendername property.
This is very "quick 'n' dirty" and will need tidying up, this is off the top o my head.
Code:
Set oNS = Appliocation.Namespace
Set oInBox = oNS.Folders.Item("Inbox")
Set MyItem = oInbox.Item(1) 'Get FirstItem

.To = MyItem.SenderName

Everybody body is somebodys Nutter.
 
nelco,

I've had chance to tidy up the code I last posted. You'll still need to determin how to Identify the mail you wish to reply to but for what it's worth her you go:
Code:
Public Sub MyReply()
    Dim oMailItem, oReplyItem       As MailItem
    Dim oNS                         As NameSpace
    Dim oFolder                     As MAPIFolder
    
    On Error GoTo Tidy_up
    
    Set oNS = Application.Session
    Set oFolder = oNS.GetDefaultFolder(olFolderInbox)
    Set oMailItem = oFolder.Items.GetFirst
    Set oReplyItem = Application.CreateItem(olMailItem)

    With oReplyItem
        .To = oMailItem.SenderName
        .Recipients.ResolveAll
        .Subject = "Received"
        .Body = "Received your application. " & _
         "Thank you for applying for our grant funds. " & _
         "Notice of acceptance for funding applications " & _
         "will be communicated to you in April, 2008. " & _
         "Any questions, please contact our office " & _
         "at 112-223-3334.  Keep this e-mail as " & _
         "record of our office having receipt of " & _
         "your grant application."
        .Display
        
    End With
    
Tidy_up:
    Set oMailItem = Nothing
    Set oReplyItem = Nothing
    Set oFolder = Nothing
    Set oNS = Nothing
    
End Sub

Everybody body is somebodys Nutter.
 
As an aside you could also do this with 'Rules and Alerts'

Everybody body is somebodys Nutter.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top