How to download a .txt file...?

T

Tim Armstrong

Hi!

How can I download a .TXT file and don't lose the formatting?
I found an example:

Code
-------------------

Option Explicit

Private Declare Function InternetGetConnectedState Lib "wininet" ( _
ByRef lpdwFlags As Long, _
ByVal dwReserved As Long) As Long

Private Declare Function InternetAutodial Lib "wininet.dll" ( _
ByVal dwFlags As Long, _
ByVal dwReserved As Long) As Long

Private Declare Function InternetAutodialHangup Lib "wininet.dll" ( _
ByVal dwReserved As Long) As Long


Private Declare Function URLDownloadToFile Lib "urlmon" _
Alias "URLDownloadToFileA" ( _
ByVal pCaller As Long, _
ByVal szURL As String, _
ByVal szFileName As String, _
ByVal dwReserved As Long, _
ByVal lpfnCB As Long) As Long

'// Ie Constants
Private Const INTERNET_CONNECTION_CONFIGURED = &H40
Private Const INTERNET_CONNECTION_LAN = &H2
Private Const INTERNET_CONNECTION_MODEM = &H1
Private Const INTERNET_CONNECTION_OFFLINE = &H20
Private Const INTERNET_CONNECTION_PROXY = &H4
Private Const INTERNET_RAS_INSTALLED = &H10
Private Const INTERNET_AUTODIAL_FORCE_ONLINE = 1
Private Const INTERNET_AUTODIAL_FORCE_UNATTENDED = 2

Private Const S_OK = &H0
Private Const E_ABORT = &H80004004
Private Const E_ACCESSDENIED = &H80070005
Private Const E_OUTOFMEMORY = &H8007000E

'// Download File constants - Amend as required
Const strDwnLd_URLColo As String = _
"http://www.interq.or.jp/sun/puremis/colo/HtmlMaker2.32.zip"
Const strDest As String = "C:\HtmlMaker2.32.zip"

Sub GetHtmlFile()
Dim DwnLoadOK As Boolean

DwnLoadOK = DownloadFile(strDwnLd_URLColo, strDest)
If Not DwnLoadOK Then
MsgBox "Error downloading " & strDwnLd_URLColo & vbCr & IeState
Else
MsgBox "Succesfully downloaded:= " & strDest
End If

'// Lets Disconect now
On Error Resume Next
InternetAutodialHangup 0

End Sub

Public Function DownloadFile( _
URL As String, _
SaveAsFileName As String) As Boolean

Dim lngRetVal As Long
DownloadFile = False
'// Lets try downloading 1st by URL
'// If Not succesful then maybe settings
'// are on Manual connect?
'// so lets try forcing it !
InternetAutodial INTERNET_AUTODIAL_FORCE_UNATTENDED, 0
lngRetVal = URLDownloadToFile(0, URL, SaveAsFileName, 0, 0)
'//
If lngRetVal = 0 Then DownloadFile = True
End Function

Private Function IeState() As String
Dim Ret As Long
Dim Msg As String

'// Retrieve the connection status
InternetGetConnectedState Ret, 0&
'// show the result
If (Ret And INTERNET_CONNECTION_CONFIGURED) = _
INTERNET_CONNECTION_CONFIGURED Then _
Msg = "Local system has a valid connection to the Internet," & _
vbCr & "but it may or may not be currently connected."
If (Ret And INTERNET_CONNECTION_LAN) = _
INTERNET_CONNECTION_LAN Then _
Msg = Msg & vbCr & "Uses a local area network" & _
"to connect to the Internet."
If (Ret And INTERNET_CONNECTION_MODEM) = _
INTERNET_CONNECTION_MODEM Then _
Msg = Msg & vbCr & "A modem is used to connect to the Internet."
If (Ret And INTERNET_CONNECTION_OFFLINE) = _
INTERNET_CONNECTION_OFFLINE Then _
Msg = Msg & vbCr & "Local system is in offline mode."
If (Ret And INTERNET_CONNECTION_PROXY) = _
INTERNET_CONNECTION_PROXY Then _
Msg = Msg & vbCr & "Uses a proxy server to connect to the Internet."
If (Ret And INTERNET_RAS_INSTALLED) = INTERNET_RAS_INSTALLED Then _
Msg = Msg & vbCr & "System has RAS installed."

If Msg <> "" Then IeState = Msg

End Function
 
J

Jake Marx

Hi Tim,

I'm not entirely sure what's going on with the download functions you're
using, but here's a guess: they're downloading the files as they would be
displayed in a browser. So you're probably losing whitespace formatting.

Here's a direct file copy (taken from my post here:
http://groups.google.com/group/micr...read/thread/d86562f72b3775a4/8b93dbf506ee96fd)
that utilizes the XMLHTTP and ADODB libraries (late binding this time so you
don't have to set a reference to the them):


Public Sub CopyFileViaHTTP(rsURL As String, _
rsFilePath As String, Optional rbOverwrite _
As Boolean = False)
Dim objXML As Object
Dim objStream As Object
Dim lOverwrite As Long

On Error GoTo ErrHandler

Set objXML = CreateObject("Msxml2.XMLHTTP")

With objXML
.Open "GET", rsURL, False
.send
If .Status >= 400 And .Status <= 599 Then _
Err.Raise 10001, "CopyFileViaHTTP", "Unable to download" _
& " file '" & rsURL & "'."
End With

Set objStream = CreateObject("ADODB.Stream")

With objStream
.Open
.Type = 1
.Write objXML.responseBody
If rbOverwrite Then
lOverwrite = 2
Else
lOverwrite = 1
End If
.SaveToFile rsFilePath, lOverwrite
.Close
End With

ExitRoutine:
If Not objStream Is Nothing Then
If objStream.State = 1 Then objStream.Close
Set objStream = Nothing
End If
Set objXML = Nothing
Exit Sub
ErrHandler:
MsgBox "Error #: " & CStr(Err.Number) & vbLf & _
"Description: " & Err.Description, vbExclamation, _
"ERROR"
Resume ExitRoutine
End Sub

--
Regards,

Jake Marx
www.longhead.com


[please keep replies in the newsgroup - email address unmonitored]
 
T

Tim Armstrong

Jake Marx,

Two questions:
1 - where I put the URL
2 - if I select the references and I need to open in other computer,
I´ll hav to active those references, ok?

Thanks for the help.
:)
 
J

Jake Marx

Hi Tim,

Tim said:
Two questions:
1 - where I put the URL

You just need to call the subroutine like this:

CopyFileViaHTTP "<URL of txt file>", "<local path for txt file>"

You can pass in True for the optional 3rd argument if you want to overwrite
an existing text file (if one exists).
2 - if I select the references and I need to open in other computer,
I´ll hav to active those references, ok?

The code I provided is late bound, so you don't need references set in your
project. The code in the previous thread (to which I linked) was early
bound, so it did require references. I'd suggest using the late bound code
(in this thread).

--
Regards,

Jake Marx
www.longhead.com


[please keep replies in the newsgroup - email address unmonitored]
 
J

Jake Marx

Hi Tim,

Tim said:
The routine function ok but lose the formatting...

I'm not sure what formatting you're talking about. A text file is a text
file - no bold, different font types/sizes...just white space and text.
I've tested my routine on several text files, and they all come back exactly
as they were created. Anyway, this code should work on any type of file (I
just tested with a Word doc, and it copied fine, too).

What type of file are you trying to download? What's the file extension?
What formatting are you losing?

--
Regards,

Jake Marx
www.longhead.com


[please keep replies in the newsgroup - email address unmonitored]
 
T

Tim Armstrong

Jake Marx,
What type of file are you trying to download?
TXT file
What's the file extension?
Test.txt (TXT extension)
What formatting are you losing?
For example, the original file is:

TEST TEST TEST TEST
TEST TEST TEST
TEST TEST
TEST

When I download the file and save in C:/, for example, the file sta
like this:

TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST


If you want that I make a file example to illustrate the problem...

Thanks again Jake
 
J

Jake Marx

Hi Tim,

I'm not sure what would cause this type of behavior. The file should come
through exactly as it was created. However, if it was created on a
different platform (ie, Unix or Mac), there may be a difference in line
terminators that's causing this problem?

Are you saying that you can create a text file on your machine, put it up on
the web, download/save it with my code, and it opens differently than the
original? What text viewer are you using?

--
Regards,

Jake Marx
www.longhead.com


[please keep replies in the newsgroup - email address unmonitored]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top