无法为“ HTMLFile”调用Body属性; vbscript中的对象 [英] cannot call the Body property for "HTMLFile" object in vbscript

查看:81
本文介绍了无法为“ HTMLFile”调用Body属性; vbscript中的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个VBSCript文件,该文件使用MSXML2库从网站获得响应;我正在尝试将响应写入HTMLFile对象的主体的innerHTML;尽管声明对象本身没有任何问题,但是当我尝试访问body属性时,VBS返回一个需要对象['OBJECT']错误。

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

当我在VBA IDE中运行此脚本时(特别是作为包含所有相关库的Excel电子表格中的模块),它可以正常工作,但是当我将其作为.vbs文件运行时返回错误。我可以采取哪些步骤解决此问题?

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?

推荐答案

您应首先初始化DOM:

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

请注意,< html>< head>< title> test< / title>< / head>< body> div> content< / div>< / body>< / html> 仅作为示例,您甚至可以使用空字符串。

Note, that <html><head><title>test</title></head><body><div>content</div></body></html> just an example, you may use even empty string.

这篇关于无法为“ HTMLFile”调用Body属性; vbscript中的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆