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
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 :
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.
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.
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..
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With