Select the product you need help with
- Internet Explorer
- Windows Phone
- More products
XL2000: How to Programmatically Perform a Web Query
Article ID: 213730 - View products that this article applies to.
This article was previously published under Q213730
A Web query is a feature in Excel that allows you to retrieve data stored on an intranet, the Internet, or the World Wide Web. A Web query can use static parameters, dynamic parameters, or a combination of both. Queries with static parameters send a query without any input; queries with dynamic parameters prompt you for input. Regardless of the type of parameters in the query, the requested information is pulled from an Internet or intranet site, and the results are placed in a worksheet.
When you use a Hypertext Markup Language (HTML) form, you can use two methods to send parameters to the server: GET and POST. Use GET when you send small amounts of information and POST when you send large amounts of information. The GET method appends the parameters to the Uniform Resource Locator (URL). The POST method sends the parameters as a separate line of text in the query file.
This article provides example macros that run Web queries that use both the GET and POST methods.
Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs.
If you have limited programming experience, you may want to contact a Microsoft Certified Partner or Microsoft Advisory Services. For more information, visit these Microsoft Web sites:
Microsoft Certified Partners - https://partner.microsoft.com/global/30000104
Microsoft Advisory Services - http://support.microsoft.com/gp/advisoryservice
For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS
Programmatically Running a Web QueryIn order to programmatically run a Web query, you must use the Add method of the QueryTables collection. When you use the Add method, it returns a QueryTable object that represents the new query table. The syntax for the Add method is as follows:
where <expression> is required and returns a QueryTable object. The Connection argument is also required and is the data source for the query table. In the context of Web queries, you can use one of two types of Connection strings: URL or FINDER. When you specify the URL type, you use the complete URL to the server on which you are performing the query (you can copy the third line from any Web query [.iqy] file and use it as the URL). When you specify the FINDER type, you use the complete path to an existing Web query (.iqy) file.
Creating a Visual Basic ModuleBefore you use the examples in this article, you must create a Visual Basic module.
Static Web Query (GET Method)The following example uses static parameters; you are not prompted for the values to send to the server. To create this macro, follow these steps:
Dynamic Web Query (POST Method)The following example uses dynamic parameters; you are prompted for the values to send to the server. This example uses the POST method because the parameters are contained in the query. To create the macro, follow these steps:
Dynamic Query (GET Method)With the FINDER type of connection, specify the full path to an existing Web query (.iqy) file. This example uses the GET method because the Web query file, Microsoft Investor Currency Rates.iqy, uses the GET method. That is, the parameters and the URL of the server are on the third line in the file. To create the macro, follow these steps:
Dynamic Query (POST Method)With the FINDER type of connection, specify the full path to an existing Web query (.iqy) file. This example uses the POST method because the Web query file, Microsoft Investor Stock Quotes.iqy, uses the POST method. That is, the parameters are on the fourth line, and the URL of the server is on the third line in the file. To create the macro, follow these steps:
For more information about Web queries, click Microsoft Excel Help on the Help menu, type create a webquery in the Office Assistant or the Answer Wizard, and then click Search to view the topic.
For more information about the Add method, click Microsoft Visual Basic Help on the Help menu, type add method in the Office Assistant or the Answer Wizard, and then click Search to view the topics returned.