cannot call the Body property for “HTMLFile” object in vbscript

こ雲淡風輕ζ 提交于 2019-12-24 08:09:04

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!