Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using OpenURL to transfer text file from website

Status
Not open for further replies.

heidi88

Programmer
May 1, 2003
32
0
0
US
I am trying to download to a local text file. Here is my code:

Private Sub Command1_Click()
Dim b() As Byte
Dim strURL As String

' Set the strURL to a valid address.
strURL = "
' Retrieve the file as a byte array.
b() = Inet1.OpenURL(strURL, icByteArray)

' ** create directory on your machine with directory target
Open "C:\FileToDownload.txt" For Binary Access _
Write As #1
Put #1, , b()
Close #1
End Sub

I actually worked, however the ouput text file has totally different format as the website. Does anyone know how to keep the web file format after the download?

Thanks a lot.
 
I could download your file using your method, but the format is all wrong. Any idea?

Thanks


 
You are not by chance dumping the results into a textbox are you? If so, it is worth pointing out that the textbox doesn't understand some of the control codes in that text file (unlike, say, the Print method, which does), and so will not display it in the same way as in your browser. Secondly, the file expects you to be using a fixed width font such as Courier.

The following illustrates the point
[tt]
Private Sub Form_Load()
Form1.Show
Form1.Print Inet1.OpenURL("End Sub
 
Thanks for the response. I only need to dump the web file into a local text file:

Private Sub Command1_Click()
Dim txt As String
Dim strURL As String

strURL = "
txt = Inet1.OpenURL(strURL)

Open "C:\rate.txt" For Output As #1
Write #1, txt
Close #1
End Sub

Any idea why the c:\rate.txt all mashed together?

Thank you
 
Here you go..

Private Sub Command1_Click()
Dim ff As Integer
Dim strURL As String
Dim strText As String

ff = FreeFile
' Set the strURL to a valid address.
strURL = "
' Retrieve the file as a byte array.
strText = Inet1.OpenURL(strURL)
strText = Replace(strText, Chr(10), vbCrLf)
' ** create directory on your machine with directory target
Open "C:\FileToDownload.txt" For Output As #ff
Print #ff, strText
Close #ff
End Sub
 
I'm assuming that you are looking at the downloaded data in Notepad. Notepad is in essence just a version of VB's textbox (or, more accurately, they are both variants on Window's Edit Control), so it suffers the same problem that I have already described - it doesn't understand how to deal with one of the control codes in the file.

LPlates solution above replaces the 'difficult' control code (ASCII 10) with one that the Edit Control does understand (vbCRLF).

However, for the sake of balance, here's a filesystemobject variant...
[tt]
With CreateObject("scripting.filesystemobject")
.CreateTextFile("c:\test.txt", True).Write Replace(Inet1.OpenURL(" Chr(10), vbCrLf)
End With
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top