Doing like so:
Shell ("C:\Program Files\Internet Explorer\iexplore.exe -embedding http://www.websiteurl.com")
Doesn't work how I need it as I essentially need it to be able to redirect and prompt a user to download a file. Any ideas?
Doing like so:
Shell ("C:\Program Files\Internet Explorer\iexplore.exe -embedding http://www.websiteurl.com")
Doesn't work how I need it as I essentially need it to be able to redirect and prompt a user to download a file. Any ideas?
There are a couple of things you could do.
Use an external program like wget
to get the file instead of IE. You can get wget for free at http://www.cygwin.com with the cygnus tools. It's GPL, so just watch out if you have a commercial product.
Write a little .NET program that uses the HttpWebRequest class to get the file and shell out to that program instead of IE. I don't think you're going to have a lot of luck shelling out to IE itself. Sounds like a, to paraphrase Steve Jobs, "bag of hurt".
If all you are trying to do is download a file, you can use URLDownloadToFile.
The Internet Explorer interface is exposed to ActiveX via the WebBrowser control (contained in %systemroot%\system32\shlwapi.dll). While it may not be very elegant, you could easily place the control somewhere off the visible area of the form.
The control is very simple to use.
Internet Explorer exposes a COM accessible interface you can use. If you really have to. I'd recommend against it - its comparatively slow, error-prone, cumbersome and resource-intensive.
What solves your problem more elegantly is using WinHTTPRequest
. In your Project, reference "Microsoft WinHTTP Services, version 5.1", and then go on like this:
Dim HttpRequest As New WinHttp.WinHttpRequest
Dim TargetUrl As String
Dim TargetFile As String
Dim FileNum As Integer
TargetFile = "C:\foo.doc"
TargetUrl = "http://www.websiteurl.com"
HttpRequest.Open Method:="GET", Url:=TargetUrl, Async:=False
HttpRequest.Send
If HttpRequest.Status = 302 Then
TargetUrl = HttpRequest.GetResponseHeader("Location")
HttpRequest.Open Method:="GET", Url:=TargetUrl, Async:=False
HttpRequest.Send
If HttpRequest.Status = "200" Then
FileNum = FreeFile
Open TargetFile For Binary As #FileNum
Put #FileNum, 1, HttpRequest.ResponseBody
Close FileNum
Debug.Print "Successfully witten " & TargetFile
Else
Debug.Print "Download failed. Received HTTP status: " & HttpRequest.Status
End If
Else
Debug.Print "Expected Redirect. Received HTTP status: " & HttpRequest.Status
End If
Hard-coding "C:\foo.doc"
does of course not make much sense. I'd use the file name the server supplies in the response headers ("Content-Type"
or "Content-Disposition"
, depending on what you expect).
Another option besides the URLDownloadToFile API call suggested by Glomek is to use the AsyncRead method built into VB6.