-1

Right now I'm trying to code a tool that shall be capable of downloading a file from a specific website, beside some other stuff.

My current solution is, that I open the website from where the file shall be downloaded in Internet Explorer, wait some seconds so the website can be loaded and then send a keystroke to Internet Explorer, which is ALT+S, to save the file to the standard download location.

However, my code fails in the line where the keystroke should be sent. You'll find it below of course plus the error it throws.


Some facts I believe you'll find useful to know:

  • My application shall be a console app, no forms app
  • I'm using the Internet Explorer for this so that I can be sure the application works for every Windows PC
  • I expect the Internet Explorer to have the default configuration for downloading files to be set to "User prompt", i.e. it's asking wether the file shall be openend or saved
  • The website, as you'll see in the code below, is a third-party website, so I don't have direct access to the files being hosted on the server

What I specifically like to know is:

  • Can I solve this problem somehow smarter? Are there other, perhaps easier ways, to download a file than what I did?
  • How can I make my application wait for the website to be loaded or even wait for the "Open - Save - Cancel" prompt to show up?
  • How do I get the "SendKeys" part to work correctly?

And most important: Please note that I'm a hobby programmer and just started with VB a few weeks ago. If I missed out some important information or my code looks weird, well, then that's why :)


Sub Download()

    Dim IE As Object
    IE = CreateObject("InternetExplorer.Application")

    Console.WriteLine("Start Download")
    IE.Navigate("https://toolslib.net/downloads/finish/1-adwcleaner/") 'Opens the website in Internet Explorer
    Do 'Waits for Internet Explorer to be launched before going on
        If CBool(Process.GetProcesses.Where(Function(P As Process) _ 
                 P.ProcessName = "iexplore").Count) Then
            Exit Do
        End If
        Threading.Thread.Sleep(100)
    Loop
    IE.Visible = True

    Threading.Thread.Sleep(5000) 'Some time for the website to load
    IE.SendKeys("%{S}", True)

End Sub

When running this code it throws the following error.

Note: Line 67 is the line where IE.SendKeys("%{S}", True) stands.

Unhandled Exception: System.ArgumentException: The process {0} wasn't found.
   at Microsoft.VisualBasic.CompilerServices.Symbols.Container.InvokeMethod(Method TargetProcedure, Object[] Arguments, Boolean[] CopyBack, BindingFlags Flags)
   at Microsoft.VisualBasic.CompilerServices.NewLateBinding.CallMethod(Container BaseReference, String MethodName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack, BindingFlags InvocationFlags, Boolean ReportErrors, ResolutionFailure& Failure)
   at Microsoft.VisualBasic.CompilerServices.NewLateBinding.ObjectLateCall(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack, Boolean IgnoreReturn)
   bei Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateCall(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack, Boolean IgnoreReturn)
   at MyApp.Module1.Download() in C:\Users\User\Documents\Visual Studio 2017\Projects\MyApp\MyApp\Module1.vb:Line 67.
Kuraiko
  • 45
  • 6
  • You should look into `WebClient.Downloadfile` – Sasha Mar 02 '18 at 11:30
  • Take a look at the `URLDownloadToFile` API https://learn.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/ms775123(v=vs.85). Also, the code appears to be VB.Net, not VBA. Can you amend the tags? – Ryan Wildry Mar 02 '18 at 14:59
  • Hello Jaxi, thanks for answer. – Kuraiko Mar 05 '18 at 13:34
  • I've looked into WebClient.Downloadfile and found this on the internet: https://msdn.microsoft.com/de-de/library/ez801hhe(v=vs.110).aspx This method generally should work, but not for me, since this requires me to add the filename of the file i want to donwload. The link I wanna use for my application alsways links to the newst version of the offered program, so the file doesn't really lie there which is why the code results in an error if I, for example, write it like this: (continues in the next comment, cause no characters left) – Kuraiko Mar 05 '18 at 13:45
  • Can't add extra paragraphs in a comment, so sorry for the bad formatting ... `Dim remoteUri As String = "http://www.toolslib.net/downloads/viewdownload/1-adwcleaner/" Dim fileName As String = "adwcleaner*.exe" [...] myWebClient.DownloadFile(myStringWebResource, fileName)` I think I'll code this thing as a forms app and hide all the stuff I don't wanna show and let it look like a slim, easy to read, console app. – Kuraiko Mar 05 '18 at 13:45
  • As for `URLDownloadToFile` it seems that this is a C, C# and C++ function? https://learn.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/ms775123(v=vs.85) – Kuraiko Mar 05 '18 at 13:50

1 Answers1

0

I used WebBrowser control in form application and its worked fine me!

                SendKeys.Send("+{TAB}")
                System.Threading.Thread.Sleep(1000)
                SendKeys.Send("{ENTER}")
  • Thanks for your answer, Sivakumar. Unfortunatly just writing SendKeys.Send(_Keys_) results in a syntax error stating that "SendKeys" hasn't been declared. – Kuraiko Mar 02 '18 at 10:55
  • In form application use like SendKeys.Send("%{S}") – Sivakumar M Mar 02 '18 at 11:12
  • @Kuraiko you are stating you're in a console app, SendKeys is part of windows forms :) – Sasha Mar 02 '18 at 11:28
  • There we have it >_< So SendKeys cannot be used in a console app. Do I have any other options to tell Internet Explorer via program code that it should save the file? – Kuraiko Mar 02 '18 at 11:43
  • This is work perfectly! Try this. System.Diagnostics.Process.Start("https://toolslib.net/downloads/finish/1-adwcleaner/") – Sivakumar M Mar 02 '18 at 12:58
  • Okay, this seems similar to what IE.Navigate("_Link_") does. The only difference is, that System.Diagnostics.Process.Start("_Link_") seems to first open IE with an empty page and then opens another IE window with the given link. Also the new window always opens maximised. – Kuraiko Mar 02 '18 at 13:22