Consulting

Results 1 to 17 of 17

Thread: Solved: How can I extract text from an e-mail???

  1. #1
    VBAX Regular
    Joined
    Aug 2005
    Posts
    7
    Location

    Question Solved: How can I extract text from an e-mail???

    I want to create a macro that will extract text from an e-mail then, write an XML file with the text from the e-mail in the XML file. Can anyone point me to a site that can get me started?? I have a good understanding of VB but, I have not written any macro's in outlook....

    Thanks

  2. #2
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    Hi and welcome to VBAX

    You're after the Body property of a mail item. How you get it depends on how you access the mail item in question. Here's an example that displays the body of a selected email[VBA]Sub GetSelectedMailText()

    Dim myOlSel As Outlook.Selection
    Dim strText As String

    'check there's something selected
    If Application.ActiveExplorer.Selection.Count = 1 Then
    Set myOlSel = Application.ActiveExplorer.Selection
    strText = myOlSel.Item(1).Body
    'do something with the mail item body text
    MsgBox strText
    End If

    End Sub[/VBA]A good thing when to do starting out with a new object model is to Hit F2 in the VBE to open the Object Browser to give you an insight into what's available and how it all fits together
    K :-)

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Hi,

    A simple sollution would be (If you have Office 2003) to copy the contents of the e-mail into a Word document and save that as XML....

    That would work very fast.
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  4. #4
    VBAX Regular
    Joined
    Aug 2005
    Posts
    7
    Location
    Thanks a lot Killian.... This is just what I needed to get me started..... Once I complete my small app. I will post it....

    Dave

  5. #5
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by d0ublej
    Thanks a lot Killian.... This is just what I needed to get me started..... Once I complete my small app. I will post it....

    Dave
    We posted simultaniously I think so I post again for you to get a notice of the tip I gave yah!
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  6. #6
    VBAX Regular
    Joined
    Aug 2005
    Posts
    7
    Location
    Thanks for your tip Joost but, I like Killian's tip since it would allow me to accomplish everything with one click. Also, if you don't mind I would like to keep this thread open until I finish writing my app. incase I run into any snags along the way. This shouldn't take me more than a week to complete. Thanks again for your help...

  7. #7
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Hi,

    No problem take all the time you need.

    Killians post gives you the body of the email in a string. Then afterwards you have to change that string in XML with some parser.

    I thought I tip you on the use of Word to get that XML done for yah in one command.

    Please do post your sollution when you've found it...I'd love to see it.
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  8. #8
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    no probs Dave, looking forward to seeing the app

    there's going be a lot more XML questions coming this way as time and MS office rolls on... might need to get my head in a book or two this autumn...
    K :-)

  9. #9
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by Killian
    there's going be a lot more XML questions coming this way as time and MS office rolls on... might need to get my head in a book or two this autumn...
    Hi Killian,

    Indeed XML is here to stay and I'm having to tutor myself on those nice parsers they have for it as well...
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  10. #10
    VBAX Regular
    Joined
    Aug 2005
    Posts
    7
    Location
    Thanks for your help guys, this went smoother than I thought. Feel free to look over the code and offer sugestions for improvement. I am trying to become a programmer and want to learn everything I can to become one....

    [VBA]
    Sub GetSelectedMailText()

    Dim myOlSel As Outlook.Selection
    Dim strText As String
    Dim intHold As Integer
    Dim intCnt As Integer
    Dim ValArray(5) As Integer

    'check there's something selected
    If Application.ActiveExplorer.Selection.Count = 1 Then
    Set myOlSel = Application.ActiveExplorer.Selection
    strText = myOlSel.Item(1).Body
    intHold = InStr(strText, "PowerBall:") 'Get character position
    intHold = intHold + 11 'Set character position for text extraction
    intCnt = 0

    Do Until intCnt = 6 'Load values into array
    strText = Mid(strText, intHold, 2)
    ValArray(intCnt) = strText
    intHold = intHold + 3
    intCnt = intCnt + 1
    strText = myOlSel.Item(1).Body
    Loop

    'Create XML file for output
    Open "C:\Documents and Settings\dsorensen\My Documents\Test App\TestData.xml" For Output As #1
    Print #1, "<?xml version=""1.0""?>"; LnRtn
    Print #1, "<numbers>"; LnRtn
    intCnt = 0
    For intCnt = 0 To 5
    Print #1, Tab; "<n" & intCnt + 1 & ">" & ValArray(intCnt) & "</n" & intCnt + 1 & ">"
    Next intCnt
    Print #1, "</numbers>"
    Close #1

    MsgBox ("Text Extraction Is Complete")

    End If

    End Sub
    [/VBA]

  11. #11
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Hi,

    Seams solid to me...thanx for posting back!

    Can you tell me what: "LnRtn" is in your code?

    Is that somekind of global var? And if so what is its value?
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  12. #12
    VBAX Regular
    Joined
    Aug 2005
    Posts
    7
    Location
    Joost,

    "LnRtn" is the command for line return... Although I noticed it is not in my loop and my XML file looks fine so, I am not sure it is needed....

  13. #13
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by d0ublej
    Joost,

    "LnRtn" is the command for line return... Although I noticed it is not in my loop and my XML file looks fine so, I am not sure it is needed....
    Hi,

    O I didn't know that must be one of many abreviations in Pure VB.

    In VBA you can't use that one but you can use (And there are many more) : vbCrLf for instance.

    But if that LnRtn works in VB then it's fine by me.

    If it's needed depends on you. If I run the code without the Line returns I get a different output then with the return. (Eg..2 extra returns when used)

    HTH,
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  14. #14
    VBAX Regular
    Joined
    Aug 2005
    Posts
    7
    Location
    Thanks for the Info....

    Cheers...

  15. #15
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    I am trying to become a programmer and want to learn everything I can to become one....
    yup, looks good to me too
    If you always know the right mail is selected, you may not need it, but a test after the InStr in case "Powerball:" isn't found would make it more robust - as would a test to make sure strText isn't empty. If you want to become a programmer, this is the kind of detail that separates code that works when you test it from code that doesn't error when somebody else starts using it
    K :-)

  16. #16
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by d0ublej
    Thanks for the Info....

    Cheers...
    Welcome..Keep it up!
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  17. #17
    VBAX Regular
    Joined
    Aug 2005
    Posts
    7
    Location
    Thanks for the tip Killian. I will incorporate error trapping. I am also thinking about modifying the code so, when this macro runs it will select an e-mail based on the sender. Also if I can get this to run once a day without me starting it, that would be great. Complete automation....

Posting Permissions

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