Results 1 to 2 of 2

Thread: Getting email body/from address....

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2002
    Location
    Philly
    Posts
    82

    Getting email body/from address....

    I want to have a Macro in Outlook 2k2 that'll......

    When new mail arrives, get me the from address, and the body of the email.

    Once I can get the data I can do the rest of the coding, but I'm stuck with this part.

    I found sample code on MSDN, but don't understand fully what is going on, and would rather not use it until I understand it.

    I altered it so far to:

    Private Sub Application_NewMail()

    Dim objItem As Outlook.MailItem
    Dim objMailItem As Outlook.MailItem
    Dim sEmailAddress As String
    Dim sSubject As String
    Dim sBody As String
    Dim fromAddress As String

    Set objItem = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Items(1)

    sBody = objItem.Body

    MsgBox (sBody)

    End Sub

    I don't get the whole OutLook.Mailitem stuff, etc., why it's being declared, what the heck it is, etc.

    And, I don't know how to get the sender email address, I can get the name, but not sure what else to do......

    Thanks a lot.....

    10k
    I know where Mulder is.....

  2. #2
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    VB Code:
    1. Dim objItem As Outlook.MailItem
    - Declare a variable which is going to hold an item of mail - (i.e. a single e-mail), this'll tell vb the type of variable which is going to be needed, so it know the size of memory it needs to set aside in order to hold this variable in.

    VB Code:
    1. Set objItem = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Items(1)
    - This sets/populates the variable. The first bit-

    Application.GetNamespace("MAPI")

    you can think of as a protocall - like SMTP, POP3 you might've heard of before, it's a way of sending e-mail from 1 place to another, exchange uses the SMTP protocall, Outlook uses the MAPI protocall so as soon as you put this, vb realises it's going to connect upto Outlook in some way.

    GetDefaultFolder(olFolderInbox).Items(1)

    Okay, imagine you open outlook yourself manually (stepping away from code for a minute) - click on the inbox folder & outlook populates the screen on the right with e-mails double click the very top e-mail to open it.

    Each of these e-mails in the list which appeared in Outlook, are called/referenced as "items" within vb - in the same way as you opened the inbox and selected the first e-mail there, this is exactly what that last bit of code was doing.

    Okay, with the e-mail message open - you have the top box saying who the e-mail was from, you might also have the senders name and bcc fields shown there too. You can see a subject line, and the main body text of the e-mail - all of these different parts/information of this particular e-mail which you can see here, are now held in the objItem variable which has just been setup/populated above.

    By entering objItem then a dot/period/full stop after it, you'll be able to see all of these parts of information about the e-mail which you can call upon. The code you've got looks up the body of the e-mail message, but you can also enter things like this to grab all the other information relating to this e-mail:

    msgbox objItem.subject
    msgbox objItem.recipient

    (Note that that's from the top of my head, I can't remeber the proper properties or whether these are strings or not, but hopefully this should explain what's happining there)!

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width