Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert ADODB binary stream to string vba

I have the following problem: I have a CSV file which is stored on a server but it has 3 characters as delimiters: "[|]". I would like to load the data from the URL and fill the data in the columns of my Excel page using the [|] as delimiter. Until now I found code to load the file from a website using an ADODB recordset but I cannot get any further:

 myURL = "http://www.example.com/file.csv"
Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
WinHttpReq.Open "GET", myURL, False, "username", "password"
WinHttpReq.send

myURL = WinHttpReq.responseBody
If WinHttpReq.Status = 200 Then
Set oStream = CreateObject("ADODB.Stream")
oStream.Open
oStream.Type = 1 'binary type
oStream.Write WinHttpReq.responseBody
oStream.SaveToFile "E:\file.csv", 2 ' 1 = no overwrite, 2 = overwrite   
oStream.Close
End If

This works fine to save a file directly. But I do not want to save it to a file, I want to enter the data in the proper cells. Is there any way to do this? I would prefer not tu use Internet Explorer objects

like image 514
g00golplex Avatar asked Nov 22 '15 22:11

g00golplex


People also ask

How to convert binary data to string in VBS?

ByteArray class lets you prepare binary data, save / restore the data from a disk, do conversion between binary and string data using several code pages (ansi, oem, utf8, ) There are at least two ways you can convert binary data (for example data from BinaryRead method) to a string. 1. Use VBS MultiByte functions :

How to create two ADOdb for one stream?

In a nutshell, you'd create two ADODB.stream objects, load data from file into both and then append to the target stream doing a Stream1.Read ()=Stream2.Write,and then save the stream to a file. Please let us know here why this post is inappropriate.

How do I read a file with ADO stream?

you can use the ADO.Stream also with local files with the LoadFromFile method and store the value into a local variable. I have here an example where this is used to read a file that uses UTF-8 code page. If the file isn't a UTF-8 one then simply delete the row with the Charset. After that you ahve the entire file content in the variable strText.

How to save/retrieve binary data?

To save/retrieve binary data from a disk, binary file upload and download in ASP or better work with binary data (different code pages-ANSI, MAC, UTF, search binary data, etc.) see Pure and Huge ASP file upload with progress..


2 Answers

Tested OK with a regular csv file:

Sub Tester()
    Dim myURL As String, txt As String, arrLines, arrVals
    Dim l As Long, v As Long, WinHttpReq As Object
    Dim rngStart As Range

    myURL = "http://www.mywebsite.com/file.csv"

    Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
    WinHttpReq.Open "GET", myURL, False, "username", "password"
    WinHttpReq.send

    txt = WinHttpReq.responseText

    'might need to adjust vbLf >> vbCrLf or vbCr
    '  depending on the file origin (Win/Unix/Mac)
    arrLines = Split(txt, vbLf)

    Set rngStart = ActiveSheet.Range("A1")

    For l = 0 To UBound(arrLines)
        arrVals = Split(arrLines(l), "[|]")
        For v = 0 To UBound(arrVals)
            rngStart.Offset(l, v).Value = arrVals(v)
        Next v
    Next l

End Sub
like image 138
Tim Williams Avatar answered Oct 01 '22 07:10

Tim Williams


you can use the ADO.Stream also with local files with the LoadFromFile method and store the value into a local variable. I have here an example where this is used to read a file that uses UTF-8 code page.

Dim adoStream As ADODB.Stream
Dim strText As String

Set adoStream = New ADODB.Stream
adoStream.Charset = "UTF-8"
adoStream.Open
adoStream.LoadFromFile "C:\Temp\Datei.txt"
strText = adoStream.ReadText

adoStream.Close
Set adoStream = Nothing

If the file isn't a UTF-8 one then simply delete the row with the Charset. After that you ahve the entire file content in the variable strText. You can then use the split() function to cut by using the delimiter.

here is how I get page content:

Dim oRequest As Object
Set oRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
oRequest.Open "GET", "http://www.cboden.de"
oRequest.Send
MsgBox oRequest.ResponseText

this should also work for CSV

like image 23
cboden Avatar answered Oct 01 '22 06:10

cboden