Search code examples
vb.netvbscriptfilesystemobject

Using FileSystemObject to Export VBScript Code to Text File


I am looking to replace the MsgBox oXMLHttp.responseText part of the below code with code that exports the results to a text file. I have checked out this post and found the following code:

Set objFSO=CreateObject("Scripting.FileSystemObject")

' How to write file
outFile="c:\test\autorun.inf"
Set objFile = objFSO.CreateTextFile(outFile,True)
objFile.Write "test string" & vbCrLf
objFile.Close

However, I have no idea how to actually implement this. Is the outFile the location and name of the file to be created? I believe it is. But then what is the purpose of objFile.Write "test string" & vbCrLf? My main question is: What am I meant to be telling the created FileSystemObject to process based on the code below?

Dim request, oXMLHttp, url
    url = "http://ws.cdyne.com/phoneverify/phoneverify.asmx"

    request = "<?xml version='1.0' encoding='utf-8'?>" & _
    "<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">" & _
    "<soap:Body>" & _
    "<CheckPhoneNumbers xmlns=""http://ws.cdyne.com/PhoneVerify/query"">" & _
    "<PhoneNumbers>" & _
    "<string >1</string>" & _
    "<string >2</string>" & _
    "<string >3</string>" & _
    "<string >4</string>" & _
    "<string >5</string>" & _
    "<string >6</string>" & _
    "<string >7</string>" & _
    "<string >8</string>" & _
    "<string >9</string>" & _
    "<string >10</string>" & _
    "</PhoneNumbers>" & _ 
    "<LicenseKey>Key</LicenseKey>" & _
    "</CheckPhoneNumbers>" & _
    "</soap:Body>" & _
    "</soap:Envelope>"

    Set oXMLHttp = CreateObject("MSXML2.ServerXMLHTTP")
    oXMLHttp.open "POST", url, False
    oXMLHttp.setRequestHeader "Content-Type", "text/xml; charset=utf-8"
    oXMLHttp.send request
    response = oXMLHttp.responseText

    MsgBox oXMLHttp.responseText

Solution

    1. Create a FileSystemObject:

      Set objFSO = CreateObject("Scripting.FileSystemObject")
      
    2. Use it to create a TextStream object representing the file you want to create (see here for the CreateTextFile function):

      Set objFile = objFSO.CreateTextFile("c:\yourfile.txt", True)
      
    3. Use the TextStream object (objFile) to write text to your new file:

      objFile.Write "some text"
      

      In your case, it looks like you'll want to write the HTTP response:

      objFile.Write oXMLHttp.responseText
      
    4. Close your file:

      objFile.Close