Sunday, February 13, 2011

How do I make Internet Explorer Open Invisibly in VB6?

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.

    Glomek : Hm... in that case, shelling out to something like cURL (http://curl.haxx.se/) may be the easiest thing to do. -o lets you specify where to save the file and -L lets you tell it to follow redirects.
    Glomek : Another thought -- You may be able to track how many times it has been downloaded using the web server logs.
    From Glomek
  • 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.

    From smbarbour
  • Your best bet is creating separate download application using some .NET http object in order to download the file. I'd recommend WebClient.

    If you really gotta stick to VB6, I'm sure you can use some basic socket work in order to download the file directly.

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

    From Tomalak
  • Another option besides the URLDownloadToFile API call suggested by Glomek is to use the AsyncRead method built into VB6.

    From MarkJ

0 comments:

Post a Comment