Pitfalls
It is very important that all open Internet connections are closed as soon as the task is completed. WinInet only allows 2 concurrent connections to a given host. If you forget to shut down the connection after use, you will experience timeouts and misleading error messages. Please refer to the following website for more information related to the maximum allowed concurrent web connections:
Howto
The source code below should be pasted in a "Class Module" in Excel. If you are not sure how to open the VBA editor in Excel for your current Microsoft Office version, please refer to the following page:
- Display the developer toolbar or ribbon in Excel
Create new class module:
- Open the Microsoft Visual Basic for Applications editor in Excel.
- Select Insert -> Class Module on the main menubar
- Rename the new class module to "WebClient"
Example
To use the code, you shold create a new instance of the class and any of the public methods:
- DownloadFile - download a specific resource to a local file
- UrlExists - check if a given URL exists
Dim objClient As New WebClient
Call objClient.DownloadFile("http://www.google.com", "c:\test.html")
Dependencies
The function "ReThrowError" is defined here:
Source Code
For the full source code, please visit