2012-02-20 11 views
0

私はvbscriptを使い慣れていません。私は自分のコードの底部にget_htmlvbscriptの "Declaration expected"

で予想誤差

宣言を取得します。私は実際に変数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がサブで、何も返さない)を括弧を使用することはできません。サブルーチンを呼び出すには、主に2つの選択肢があります。 sub_proc param1, param2またはCall sub_proc(param1, param2)
  • 代入演算子 '='でオブジェクトが十分ではありません。 Setステートメントを使用する必要があります。変数 にオブジェクト参照を割り当てます。

応答はutf-8エンコードとして返される場合があります。しかし、FSOはutf-8と平和ではありません。別のオプションは、応答をユニコードとして書くことです(CreateTextFileに第3のパラメータとしてTrueを渡す) ですが、出力サイズはそれよりも大きくなります。したがって、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

+0

'code' Sub_proc get_html(ByVal up_http、ByVal down_http) ? – user1086978

+0

これはちょうど "サブアドレスを呼び出す"構文サンプルであり、 'sub_proc'はサンプルサブネームだけです。 –

1

あなたはおそらく、あなたのMainサブルーチン(Sub Main())内からの呼び出しであることをget_htmlにお電話を移動したいです。たとえば、

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

AFAIKでは、モジュール内で直接関数呼び出しを行うことはできません。

+0

私はこのエラーを取得:引数パラメータに指定されていない「公共のサブget_htmlのdown_htttp(オブジェクト、オブジェクトとしてdown_httpとしてup_http」)。あなたが言ったようにコードを変更した後。あなたの助けをありがとう – user1086978

関連する問題