download from internet

R

Richard

Hi

Am I able to download a certain file from a website through code?

Thanks in advance

Richard
 
N

Naresh Nichani MVP

Hi:

Try this in a Module --

Option Explicit

Private Declare Function ShellExecute Lib "shell32.dll" Alias
"ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal
lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String,
ByVal nShowCmd As Long) As Long

Sub GoToWebPage(lngHandle As Long, cURL As String)

'lngHandle is Handle of Form calling this and cURL is URL to navigate to

On Error GoTo errHandler
Dim nRet As Long
Dim SW_Normal As Variant
Const ERROR_FILE_NOT_FOUND = 2&
Const ERROR_PATH_NOT_FOUND = 3&
Const ERROR_BAD_FORMAT = 11&
Const SE_ERR_NOASSOC = 31
Const SE_ERR_OOM = 8



nRet = ShellExecute(lngHandle, "open", cURL, vbNull, vbNullString,
SW_Normal)
If nRet <= 32 Then
'error that can be returned by ShellExecute are:
'ERROR_FILE_NOT_FOUND = 2&
'ERROR_PATH_NOT_FOUND = 3&
'ERROR_BAD_FORMAT = 11&
'SE_ERR_NOASSOC = 31
'SE_ERR_OOM = 8
Select Case nRet

Case ERROR_FILE_NOT_FOUND
MsgBox "File not found !", vbExclamation

Case ERROR_PATH_NOT_FOUND
MsgBox "Path not found !", vbExclamation

Case ERROR_BAD_FORMAT
MsgBox "Bad format", vbExclamation

End Select
Else
'the browser is runing
End If

Exit Sub
errHandler:
'Call modGlobal.GenericErrorHandler("GotoWEbPage", Err.Description,
Err.Number)
End Sub

To call it from a button anywhere --
Private Sub Command1_Click()
Call Module1.GoToWebPage(Me.hwnd, "http://www.yoursite.com/filename.zip")
End Sub

Regards,

Naresh Nichani
Microsoft Access MVP
 
R

Richard

Thanks Naresh

Works like a charm

Richard

Naresh Nichani MVP said:
Hi:

Try this in a Module --

Option Explicit

Private Declare Function ShellExecute Lib "shell32.dll" Alias
"ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal
lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String,
ByVal nShowCmd As Long) As Long

Sub GoToWebPage(lngHandle As Long, cURL As String)

'lngHandle is Handle of Form calling this and cURL is URL to navigate to

On Error GoTo errHandler
Dim nRet As Long
Dim SW_Normal As Variant
Const ERROR_FILE_NOT_FOUND = 2&
Const ERROR_PATH_NOT_FOUND = 3&
Const ERROR_BAD_FORMAT = 11&
Const SE_ERR_NOASSOC = 31
Const SE_ERR_OOM = 8



nRet = ShellExecute(lngHandle, "open", cURL, vbNull, vbNullString,
SW_Normal)
If nRet <= 32 Then
'error that can be returned by ShellExecute are:
'ERROR_FILE_NOT_FOUND = 2&
'ERROR_PATH_NOT_FOUND = 3&
'ERROR_BAD_FORMAT = 11&
'SE_ERR_NOASSOC = 31
'SE_ERR_OOM = 8
Select Case nRet

Case ERROR_FILE_NOT_FOUND
MsgBox "File not found !", vbExclamation

Case ERROR_PATH_NOT_FOUND
MsgBox "Path not found !", vbExclamation

Case ERROR_BAD_FORMAT
MsgBox "Bad format", vbExclamation

End Select
Else
'the browser is runing
End If

Exit Sub
errHandler:
'Call modGlobal.GenericErrorHandler("GotoWEbPage", Err.Description,
Err.Number)
End Sub

To call it from a button anywhere --
Private Sub Command1_Click()
Call Module1.GoToWebPage(Me.hwnd, "http://www.yoursite.com/filename.zip")
End Sub

Regards,

Naresh Nichani
Microsoft Access MVP
 
Top