View Single Post
  #13   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.excel
Robert Baer Robert Baer is offline
external usenet poster
 
Posts: 93
Default Read (and parse) file on the web CORRECTION#2

Auric__ wrote:
Robert Baer wrote:

And assuming a fix, what can i do about the OPEN command/syntax?
// What i did in Excel:
S$ = "D:\Website\Send .Hot\****"
tmp = Environ("TEMP")& "\"& S$


The contents of the variable S$ at this point:

S$ = "C:\Users\auric\D:\Website\Send .Hot\****"

Do you see the problem?

Also, as Garry pointed out, cleanup should happen automatically. The "Kill"
keyword deletes files.

Try this code:

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

Function downloadFile(what As String) As String
'returns tmp file's path on success or empty string on failure
Dim tmp, result, contents, fnum
tmp = Environ("TEMP")& "\"& Format$(Now, "yyyymmdd-hhmmss-")& _
"downloaded.tmp"
'download
result = URLDownloadToFile(0, what, tmp, 0, 0)
If result< 0 Then
'failed to download; error handler here, if any
On Error Resume Next 'can be avoided by checking if the file exists
Kill tmp 'cleanup
On Error GoTo 0
downloadFile = ""
Else
'read from file
fnum = FreeFile
Open tmp For Binary As fnum
contents = Space$(LOF(fnum))
Get #fnum, 1, contents
Close fnum
downloadFile = tmp
End If
End Function

Sub foo()
Dim what As String, files_to_get As Variant, L0
'specify files to download
files_to_get = Array("http://www.oil4lessllc.com/****", _
"http://www.oil4lessllc.org/gTX.htm")
For L0 = LBound(files_to_get) To UBound(files_to_get)
what = downloadFile(files_to_get(L0))
If Len(what) Then
'****parse file here*****
Kill what 'cleanup
End If
Next
End Sub

Grumble..do not understand well enough to get working.
Now i do not know what i had that fully worked with the gTX.htm file.

The following "almost" works; it fails on the open.

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

Sub foo()
Dim tmp, result, content$
SRC1$ =
"https://www.nsncenter.com/NSNSearch?q=5960%20regulator&PageNumber=5"
SRC3$ = "https://www.nsncenter.com/NSNSearch?q=5960"
SRC2$ = "https://www.nsncenter.com/NSN/5960" 'example only
SRCA$ = "https://www.nsncenter.com/"
SRCB$ = "NSNSearch?q=5960%20regulator&PageNumber=5"
FSC$ = "/NSN/5960"

'Kill "C:\DOCUME~1\ROBERT~1\LOCALS~1\Temp\SRC1$"
S$ = "D:\Website\Send .Hot\****" ' "result" passes
'S$ = "http://www.oil4lessllc.com/REACH.PDF" ' "result" passes
'S$ = "http://www.oil4lessllc.org/gTX.htm" ' "result" passes

tmp = Environ("TEMP") & "\" & S$
' The file "tmp" exists ONLY if the value of "result" is zero
'download
result = URLDownloadToFile(0, S$, tmp, 0, 0)

If result < 0 Then
'failed to download; error handler here
Stop
Else
'read from file
Open tmp For Binary As 1 ' Bad file name or number
content$ = Space$(LOF(1))
lim = LOF(1)
Get #1, 1, content$
Close 1
'parse file here
For pick = 1 To lim Step 50
L1$ = Mid$(content$, pick, 50)
x = x
Next pick
'[...]
'cleanup
End If
Kill tmp
End Sub