2012-02-20 76 views
0

我是vbscript的新手。我得到的错误“声明预计”在vbscript

宣言预计get_html

在我的代码的底部。我实际上是试图为变量get_html声明一个值(这是一个url)。我该如何解决这个问题?

Module Module1 

Sub Main() 

End Sub 
Sub get_html(ByVal up_http, ByVal down_http) 
    Dim xmlhttp : xmlhttp = CreateObject("msxml2.xmlhttp.3.0") 
    xmlhttp.open("get", up_http, False) 
    xmlhttp.send() 

    Dim fso : fso = CreateObject("scripting.filesystemobject") 

    Dim newfile : newfile = fso.createtextfile(down_http, True) 
    newfile.write(xmlhttp.responseText) 

    newfile.close() 

    newfile = Nothing 
    xmlhttp = Nothing 

End Sub 
get_html _"http://www.somwwebsite.com", _"c:\downloads\website.html" 

End Module 

回答

4

存在一些语法错误。

  • 模块声明不是VBScript的一部分。
  • 下划线可能会导致意想不到的结果。见http://technet.microsoft.com/en-us/library/ee198844.aspx(搜索单词underscore页)
  • 调用子时,不能使用括号(例如xmlhttp.open是一分,不返回任何东西)。调用子例程有两个主要的选择。 sub_proc param1, param2Call sub_proc(param1, param2)
  • 赋值运算符'='对于对象是不够的。你应该 使用Set声明。它将对象引用分配给 变量。

响应可能会以utf-8编码返回。但是,FSO与UTF-8并不和谐。另一种选择是将响应写为unicode(将True作为第三个参数传递给CreateTextFile) 但输出大小将比应该大。因此我宁愿使用Stream对象。
我修改了你的代码。请考虑。

'Requeired Constants 
Const adSaveCreateNotExist = 1 'only creates if not exists 
Const adSaveCreateOverWrite = 2 'overwrites or creates if not exists 
Const adTypeBinary = 1 

Sub get_html(ByVal up_http, ByVal down_http) 
    Dim xmlhttp, varBody 
    Set xmlhttp = CreateObject("msxml2.xmlhttp.3.0") 
     xmlhttp.open "GET", up_http, False 
     xmlhttp.send 
     varBody = xmlhttp.responseBody 
    Set xmlhttp = Nothing 
    Dim str 
    Set str = CreateObject("Adodb.Stream") 
     str.Type = adTypeBinary 
     str.Open 
     str.Write varBody 
     str.SaveToFile down_http, adSaveCreateOverWrite 
     str.Close 
    Set str = Nothing 
End Sub 

get_html "http://stackoverflow.com", "c:\downloads\website.html" 
+0

我得到这个错误不能使用括号,当调用一个Sub,我该怎么做来解决它,请帮助我是一个初学者 – user1086978 2012-02-20 07:40:24

+0

'代码'Sub_proc get_html(ByVal up_http,ByVal down_http)是这样的? – user1086978 2012-02-20 09:28:16

+0

它只是“调用子”语法示例,而“sub_proc”只是一个示例子名。 – 2012-02-20 23:57:39

1

你可能想你的电话从你Main子程序(Sub Main())内移动到get_html是一个电话。例如:

Sub Main() 
    get_html _"http://www.somwwebsite.com", _"c:\downloads\website.html" 
End Sub 

AFAIK,您不能直接在模块内进行函数调用。

+0

我得到这个错误:参数的参数未指定 '公用Sub get_html的down_htttp(up_http为对象,down_http作为对象)'。改变你的代码后,像你说的。感谢您的帮助 – user1086978 2012-02-20 03:37:30