Re: How to download a .txt file...?



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/microsoft.public.excel.programming/browse_thread/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]


Tim Armstrong wrote:
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

--------------------

But this example lost the formating of .TXT file...

Thanks a lot for any help.


.



Relevant Pages

  • Question for Randy Birch About Download File
    ... I am using the following code from Randy's site to download some extra files ... DownloadTempRegKey As String 'temporary download destination folder ... Private Declare Function RegQueryValueEx Lib "advapi32.dll" _ ... Dim dldata As FileRegistryDownloadData ...
    (microsoft.public.vb.general.discussion)
  • Re: Internet download APIs only get first part of file, rest is blank
    ... filling up the rest of the string. ... Code for the download function follows ... ... Private Declare Function InternetCloseHandle Lib "wininet" (ByVal hInet As ...
    (microsoft.public.vb.general.discussion)
  • Re: Linking/importing to a table http://
    ... The following code should be able to download the file for you, though, so ... Private Declare Function URLDownloadToFile Lib "urlmon" _ ... ByVal szFileName As String, ByVal dwReserved As Long, _ ...
    (microsoft.public.access.externaldata)
  • Re: How to download a file from the internet using inet control
    ... How better I could download this using the source code. ... Private Declare Function URLDownloadToFile _ ... LocalFilename As String) As Boolean ... Private Sub Form_Load ...
    (microsoft.public.vb.general.discussion)
  • Re: CryptAPI
    ... > Private Declare Function CryptAcquireContext Lib "advapi32.dll" Alias ... > As String, ByVal dwProvType As Long, ByVal dwFlags As Long) As Long ... > On Error GoTo ErrSign ...
    (microsoft.public.vb.winapi)