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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get HTML Email body into IE document object

Status
Not open for further replies.

fredmartinmaine

IS-IT--Management
Mar 23, 2015
79
US
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
 
Use the lighter weight MSHTML library, and then something like:

Code:
[blue]    Set HTMLDoc = CreateObject("HTMLFile")
    HTMLDoc.body.innerHTML = objEmail.htmlBody[/blue]

You should then be able to parse HTMLDoc using getelement methods
 
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
 
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.
 
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

[tt]HTMLDoc.Write objEmail.htmlBody
HTMLDOC.Close[/tt]

Instead of

[tt]HTMLDoc.body.innerHTML = objEmail.htmlBody[/tt]

 
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.
 
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.
 
<grrrr> Seem to be running into challenges with the way VBScript handles HTMLDocument objects
 
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")
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top