×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Get HTML Email body into IE document object

Get HTML Email body into IE document object

Get HTML Email body into IE document object

(OP)
I have a need to open Outlook emails, which contain HTML tables. I need to parse some data out of the tables.

I have the email in an object, and can access the HTML body:

Set objOutlook = CreateObject("Outlook.Application")
Set objEmail = objOutlook.CreateItemFromTemplate(EmailFile)

MsgBox(objEmail.body)
MsgBox(objEmail.htmlbody

Unless I'm headed down the wrong path, I'd like to get that htmlbody into an IE.document so I can use some of the GetElement methods to retrieve the data I need.

Can someone point me in the right direction, or to some documentation?

Thanks
Fred

RE: Get HTML Email body into IE document object

Use the lighter weight MSHTML library, and then something like:

CODE

    Set HTMLDoc = CreateObject("HTMLFile")
    HTMLDoc.body.innerHTML = objEmail.htmlBody 

You should then be able to parse HTMLDoc using getelement methods

RE: Get HTML Email body into IE document object

(OP)
Thanks. I started in with it but got some unexpected errors, could have been my poor setup of the example. But, I'm temporarily assigned to work on a different project but will be back on this soon. I'll post back.
Fred

RE: Get HTML Email body into IE document object

(OP)
I'm back at this now. Here's what I have in total:

CODE -->

EmailFile2 = "H:\Projects\Customer\Loss_Control_Inspection.msg"

Set objOutlook = CreateObject("Outlook.Application")
Set objEmail = objOutlook.CreateItemFromTemplate(EmailFile2)

Set OutFile = objFSO.CreateTextFile("H:\Projects\Customer\TEST2.html",TRUE)
OutFile.writeline(objEmail.htmlbody)

Set HTMLDoc = CreateObject("HTMLFile")
HTMLDoc.body.innerHTML = objEmail.htmlBody 

I get the runtime error: Object required: 'HTMLDoc.body' 800A01A8

TEST2.html was just to prove that I was looking at the right data. I found several cases of the same error in forums on the web, but no solutions. Do I need to invoke MSHTML in some way?

Thanks.

RE: Get HTML Email body into IE document object

Possibly my fault. I tested the code in VBA rather than VBScript because of the useful IDE - but sometimes VBScript works a bit differently to VBA.

Try

HTMLDoc.Write objEmail.htmlBody
HTMLDOC.Close


Instead of

HTMLDoc.body.innerHTML = objEmail.htmlBody

RE: Get HTML Email body into IE document object

(OP)
No worries. In the meantime I stumbled upon these two things which both work although I can't say I understand why. One of them matches yours:

CODE -->

Set document = CreateObject("htmlfile")
document.write objEmail.htmlBody 

CODE -->

Set oHTMLDoc = CreateObject("htmlfile")
oHTMLDoc.open
oHTMLDoc.close
oHTMLDoc.body.innerHTML = objEmail.htmlBody 

Thanks.

RE: Get HTML Email body into IE document object

(OP)
I'm afraid someone will have to hit me over the head with an example; I'm struggling with usage and I'm still getting errors like 'object required' and 'object doesn't support this property or method'.

CODE -->

<table>
  <tr>
    <td class='text' style='margin-left:10px; margin-top:5px; vertical-align:top; width:300px'><b>Policy/Quote#:</b> PMP0975891</td>
	<td class='text' style='margin-left:20px; margin-top:5px; vertical-align:top; width:180px'><b>Type of Risk: </b></td>
	<td class='text' style='margin-top:5px; vertical-align:top; width:500px'>Mixed types including Office</td>
  </tr>
</table> 

Assume the above fragment in the html body of that email. I'm trying to extract the policy number "PMP0975891".
If I can extract the string "<b>Policy/Quote#:</b> PMP0975891" then I can deal with it.

I figured I'd get a list of elements with class 'text' and from that, find the one containing the value "Policy/Quote#:".

Is that reasonable? In any case, right out of the gate I'm having trouble getting the list of objects. Samples I've found online aren't helping me. I've tried stuff like:

CODE -->

Set EList = HTMLDoc.body.innerHTML.getElementsByClassName("text")
Set EList = HTMLDoc.innerHTML.getElementsByClassName("text")
Set EList = HTMLDoc.getElementsByClassName("text")
Set EList = HTMLDoc.body.getElementsByClassName("text") 

Thanks in advance.

RE: Get HTML Email body into IE document object

<grrrr> Seem to be running into challenges with the way VBScript handles HTMLDocument objects

RE: Get HTML Email body into IE document object

(OP)
Equally frustrated. For what it's worth here's a code snippet that I found online that actually produces some results without errors. Still feels a long way from where I need to be.

CODE -->

Set document = CreateObject("htmlfile")
document.write "<html><head><title>test</title></head><body><div id='yo'>hello world</div><table><tr><td>Col-A</td><td>Col-B</td></tr></table></body></html>"
MsgBox document.GetElementById("yo").innerHTML
Set allresults = document.getElementsByTagName("td")
For Each result In allresults
	msgbox("got an object")
	msgbox(result.innerhtml)
Next
MsgBox("done") 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close