Extracting Web Files to local directory

Extracting Web Files to local directory

am 24.10.2007 23:54:26 von TerryJayFoster

I have a table of file names for PDF documents on the web. I need to
be able to pass the URL and have the document retrieved from the web
site and saved in a local directory.

Example:

I have a table with PDF document names:

Doc1.pdf
Doc2.pdf
Doc3.pdf . . .

I have a website where all of the PDF documents are stored:

Http://www.MySite.info/PDF

I have a form where the hyperlink is concatenated properly, "http://
www.mysite.info/pdf/Doc1.pdf"

If they click on the link, a browser window opens up and displays the
PDF just fine. I want to add a button that, instead of opening the
browser, would just save the file to a local directory.

I went this direction, but think my approach is probably all wrong:

Function ExtractIt(strURL As String, strFileName As String)
Dim objWeb As Object
Dim intFile As Integer
Dim strFile As String
Dim strHTML As String
Dim strURL As String

Set objWeb = CreateObject("Microsoft.XMLHTTP")
objWeb.Open "GET", strURL & "/" & strFileName, False
objWeb.send

strHTML = objWeb.responseText
strFile = CurrentProject.Path & strFileName

intFile = FreeFile()
Open strFile For Output As #intFile
Print #intFile, strHTML
Close #intFile

End Function

Re: Extracting Web Files to local directory - ANSWER

am 25.10.2007 16:23:23 von TerryJayFoster

On Oct 24, 4:54 pm, TerryJayFos...@gmail.com wrote:
> I have a table of file names for PDF documents on the web. I need to
> be able to pass the URL and have the document retrieved from the web
> site and saved in a local directory.
>
> Example:
>
> I have a table with PDF document names:
>
> Doc1.pdf
> Doc2.pdf
> Doc3.pdf . . .
>
> I have a website where all of the PDF documents are stored:
>
> Http://www.MySite.info/PDF
>
> I have a form where the hyperlink is concatenated properly, "http://www.mysite.info/pdf/Doc1.pdf"
>
> If they click on the link, a browser window opens up and displays the
> PDF just fine. I want to add a button that, instead of opening the
> browser, would just save the file to a local directory.
>
> I went this direction, but think my approach is probably all wrong:
>
> Function ExtractIt(strURL As String, strFileName As String)
> Dim objWeb As Object
> Dim intFile As Integer
> Dim strFile As String
> Dim strHTML As String
> Dim strURL As String
>
> Set objWeb =CreateObject("Microsoft.XMLHTTP")
> objWeb.Open "GET", strURL & "/" & strFileName, False
> objWeb.send
>
> strHTML = objWeb.responseText
> strFile = CurrentProject.Path & strFileName
>
> intFile = FreeFile()
> Open strFile For Output As #intFile
> Print #intFile, strHTML
> Close #intFile
>
> End Function
I needed to use the stream object.
My Answer:

Function Extract()
Dim objWeb As Object
Dim strFile As String
Dim objHTML As Object
Dim strSQL As String
Dim rsLOC As ADODB.Recordset
Dim dbLOC As ADODB.Connection
Dim strURL As String
Dim objBin As Object

Set rsLOC = New ADODB.Recordset
With rsLOC
.ActiveConnection = CurrentProject.Connection
.CursorType = adOpenKeyset
.LockType = adLockOptimistic
.Source = "Select * from dc;"
.Open
Do Until .EOF
If Not IsNull(!pdf) Then
Set objWeb = CreateObject("Microsoft.XMLHTTP")
Set objBin = CreateObject("ADODB.Stream")
objBin.Type = adTypeBinary

strURL = "http://www.mysite.info/pdf/"
strURL = strURL & !FileName
Debug.Print strURL
objWeb.Open "GET", strURL, False
objWeb.Send
objBin.Open
objBin.Write objWeb.responseBody
'Debug.Print strHTML
strFile = CurrentProject.Path & "\PDF\" & !FileName
objBin.SaveToFile strFile, adSaveCreateOverWrite
Debug.Print strFile
.MoveNext
End If
DoEvents
Loop
.Close
End With
Set rsLOC = Nothing


End Function