PDA

View Full Version : Solved: How can I extract text from an e-mail???



d0ublej
08-15-2005, 02:53 PM
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

Killian
08-16-2005, 02:24 AM
Hi and welcome to VBAX :hi:

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 emailSub 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 SubA 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

MOS MASTER
08-16-2005, 10:03 AM
Hi, :yes

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

d0ublej
08-16-2005, 10:04 AM
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 :beerchug:

MOS MASTER
08-16-2005, 10:05 AM
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 :beerchug:

We posted simultaniously I think so I post again for you to get a notice of the tip I gave yah! :whistle:

d0ublej
08-16-2005, 11:00 AM
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...

MOS MASTER
08-16-2005, 11:09 AM
Hi, :yes

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

Killian
08-16-2005, 12:08 PM
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...

MOS MASTER
08-16-2005, 12:18 PM
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, :hi:

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

d0ublej
08-16-2005, 04:07 PM
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....:)


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

MOS MASTER
08-16-2005, 04:15 PM
Hi, :yes

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

d0ublej
08-16-2005, 04:44 PM
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....

MOS MASTER
08-17-2005, 11:12 AM
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, :yes

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

d0ublej
08-17-2005, 11:35 AM
Thanks for the Info....

Cheers...:beerchug:

Killian
08-17-2005, 11:40 AM
I am trying to become a programmer and want to learn everything I can to become one....yup, looks good to me too :thumb
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 :whistle:

MOS MASTER
08-17-2005, 11:42 AM
Thanks for the Info....

Cheers...:beerchug:

Welcome..Keep it up! :thumb

d0ublej
08-17-2005, 02:42 PM
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....:cloud9: