2012-02-20 75 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