Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default How to download a .txt file...?


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

--
Tim Armstron

-----------------------------------------------------------------------
Tim Armstrong's Profile: http://www.excelforum.com/member.php...fo&userid=3716
View this thread: http://www.excelforum.com/showthread.php?threadid=56937

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 860
Default 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 he
http://groups.google.com/group/micro...93dbf506ee96fd)
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.



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default How to download a .txt file...?


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.
:)


--
Tim Armstrong


------------------------------------------------------------------------
Tim Armstrong's Profile: http://www.excelforum.com/member.php...o&userid=37161
View this thread: http://www.excelforum.com/showthread...hreadid=569379

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 860
Default How to download a .txt file...?

Hi Tim,

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


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default How to download a .txt file...?


Jake Marx,

The routine function ok but lose the formatting...



Thanks.


--
Tim Armstrong


------------------------------------------------------------------------
Tim Armstrong's Profile: http://www.excelforum.com/member.php...o&userid=37161
View this thread: http://www.excelforum.com/showthread...hreadid=569379



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 860
Default How to download a .txt file...?

Hi Tim,

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


  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default How to download a .txt file...?


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

--
Tim Armstron

-----------------------------------------------------------------------
Tim Armstrong's Profile: http://www.excelforum.com/member.php...fo&userid=3716
View this thread: http://www.excelforum.com/showthread.php?threadid=56937

  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 860
Default How to download a .txt file...?

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]


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



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
download file of 200 name, addr.etc to excel file - how? please ad Tony V. Excel Worksheet Functions 1 October 24th 08 09:50 AM
HELP----Can't get a CSV file to download onto my PC Bubey New Users to Excel 2 June 8th 06 02:42 AM
How to write a micro to download file at a URL save the file at the local PC www.microsoft.com Excel Programming 2 March 9th 06 02:21 AM
I need to download an exel spreadsheet file. (file extension :xls) buckrogers Excel Discussion (Misc queries) 2 December 8th 04 11:08 PM
using vba to download file via ftp daryl Excel Programming 3 January 29th 04 07:58 AM


All times are GMT +1. The time now is 12:54 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"