问题
I have a VBSCript File that uses the MSXML2 library to get a response from a website; i'm trying to write that response to an HTMLFile object's body's innerHTML; although the object itself is declared with no issues whatsoever, VBS returns an "OBJECT REQUIRED ['OBJECT']" Error when I try to access the body property.
Here's the code I have so far.
const URL = "https://mywebsite.aspx"
Dim o,user,pass,html
Set o = CreateObject("MSXML2.ServerXMLHTTP.6.0")
o.open "GET", URL , False
o.send
'Error occurs here:
html = CreateObject("htmlFile")
html.body.innerHTML= o.responseText
When I run this script in a VBA IDE (specifically, as a module in an Excel spreadsheet with all relevant libraries included), it works fine, but returns an error when I run it as a .vbs file. What steps can I take to fix this?
回答1:
You should initialize DOM first:
Set document = CreateObject("htmlfile")
document.write "<html><head><title>test</title></head><body><div>content</div></body></html>"
MsgBox document.body.innerHTML
Or
Set document = CreateObject("htmlfile")
document.open
document.close
document.body.innerHTML = "<html><head><title>test</title></head><body><div>content</div></body></html>"
MsgBox document.body.innerHTML
Note, that <html><head><title>test</title></head><body><div>content</div></body></html> just an example, you may use even empty string.
来源:https://stackoverflow.com/questions/53938629/cannot-call-the-body-property-for-htmlfile-object-in-vbscript