user1086978
user1086978

Reputation: 3

"Declaration expected" in vbscript

I'm new to vbscript. I get the error

Declaration expected at get_html

At the bottom part of my code. I am actually trying to declare a value (which is a url) for the variable get_html. How can I resolve this?

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

Upvotes: 0

Views: 1419

Answers (2)

Kul-Tigin
Kul-Tigin

Reputation: 16950

There are some syntax mistakes.

  • Module statement is not part of VBScript.
  • Underscores can lead to unexpected results. See http://technet.microsoft.com/en-us/library/ee198844.aspx (search for the word underscore on page)
  • You can't use parentheses when calling a Sub (e.g. xmlhttp.open is a sub, does not return anything). You have two main alternatives to calling a sub routine. sub_proc param1, param2 or Call sub_proc(param1, param2)
  • The assignment operator '=' is not enough for the objects. You should use Set statement. It assigns object references to the variables.

The response may be return as utf-8 encoded. But however FSO is not at peace with utf-8. Another option is to write the response as unicode (passing True as third parameter to CreateTextFile) but the output size will be larger than it should be. Therefore I would prefer to use Stream object.
I've revised your code. Please consider.

'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"

Upvotes: 4

ziesemer
ziesemer

Reputation: 28687

You probably want to move your call to get_html to be a call from within your Main subroutine (Sub Main()). For example:

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

AFAIK, you can't make function calls from directly within a module.

Upvotes: 1

Related Questions