XL98: How to Programmatically Perform a Web Query

ID: Q187364


The information in this article applies to:
  • Microsoft Excel 98 Macintosh Edition


SUMMARY

A Web query is a new feature in Microsoft Excel 98 Macintosh Edition that allows you to retrieve data stored on an intranet or on the Internet. 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 intranet or Internet site, and the results are placed in a worksheet.

Using a Hypertext Markup Language (HTML) form, you can use two methods to send parameters to the server: GET and POST. Use GET when you are sending a small amount of information and POST when you are sending larger amount 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 sample macros that run Web queries that use both the GET and the POST methods.


MORE INFORMATION

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 the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

http://www.microsoft.com/support/supportnet/overview/overview.asp

Programmatically Running a Web Query

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:
<expression>.Add(Connection, Destination, SQL)
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 Module

Before you can use the examples in this article, you must create a module. To do this, follow these steps:

  1. Save and close any open workbooks, and then create a new workbook.


  2. Start the Visual Basic Editor by pressing OPTION+F11.


  3. On the Insert menu, click Module.


URL Examples

Static Web Query (GET Method):

In the following sample that uses static parameters, you are not prompted for the values to send to the server. To create this macro, follow these steps:

  1. In the module, type the following code:
    
       Sub URL_Get_Query()
    
           With ActiveSheet.QueryTables.Add(Connection:= _
           "URL;http://webservices.pcquote.com/cgi-" & _
           "bin/excelget.exe?TICKER=msft", _
           Destination:=Range("a1"))
    
              .BackgroundQuery = True
              .TablesOnlyFromHTML = True
              .Refresh BackgroundQuery:=False
              .SaveData = True
              End With
       End Sub 


  2. Switch to Sheet1 in the workbook.


  3. Run the URL_Get_Query macro.


A detailed stock quote for MSFT is returned to Sheet1.

Dynamic Web Query (POST Method):

In the following sample that uses dynamic parameters, you are prompted for the values to send to the server. Because the parameters are contained in the query, this sample uses the POST method. To create the macro, follow these steps:

  1. Switch to the Visual Basic Editor and type the following macro in the module sheet:
    
          Sub URL_Post_Query()
    
             With ActiveSheet.QueryTables.Add(Connection:= _
             "URL;http://webservices.pcquote.com/cgi-bin/excel.exe", _
             Destination:=Range("a1"))
    
                .PostText = _
                "QUOTE0=[""QUOTE0"",""Enter up to 20 symbols " & _
                "separated by spaces.""]"
    
                .BackgroundQuery = True
                .TablesOnlyFromHTML = True
                .Refresh BackgroundQuery:=False
                .SaveData = True
             End With
          End Sub 


  2. Switch to Sheet2 in the workbook and run the URL_Post_Query macro.


  3. In the Enter Parameter Value dialog box, type up to twenty valid stock symbols that are separated by a space; for example, type msft.


  4. Click OK.


The stock quotes for the stock symbols that you entered are returned into a table in Sheet2.

FINDER Samples

Dynamic Query (GET Method):

With the FINDER type of connection, specify the full path to an existing Web query file. This sample uses the GET method because the Web query file, "Microsoft Investor Indices," 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:

  1. Switch to the Visual Basic Editor and type the following macro in the module sheet:
    
       Sub Finder_Get_Query()
    
          Dim IQYFile As String
    
          ' NOTE: Change <HD Name> in the line below to the name of your
          ' hard disk.
          IQYFile = "<HD Name>:Microsoft Office 98:Office:Queries:" & _
                    "Microsoft Investor Indices"
    
          With ActiveSheet.QueryTables.Add(Connection:= _
          "FINDER;" & IQYFile, Destination:=Range("A1"))
    
             .BackgroundQuery = True
             .TablesOnlyFromHTML = True
             .Refresh BackgroundQuery:=False
             .SaveData = True
          End With
        End Sub 


  2. Switch to Sheet3 in the workbook and run the Finder_Get_Query macro.

    Because this example uses a dynamic parameter, you are prompted for the parameter value.


  3. In the Enter Parameter Value dialog box, type a valid stock symbol.


  4. Click OK.


A detailed stock quote is returned to Sheet3 for the stock symbol you entered in step 3.

Dynamic Query (POST Method):

With the FINDER type of connection, specify the full path to an existing Web query file. This example uses the POST method because the Web query file, "Microsoft Investor Stock Quote," 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:

  1. Switch to the Visual Basic Editor and type the following macro in the module sheet:
    
            Sub Finder_Post_Query()
    
              Dim IQYFile As String
    
              ' NOTE: Change <HD Name> in the line below to the name of your
              ' hard disk.
              IQYFile = "<HD Name>:Microsoft Office 98:Office:Queries:" & _
                        "Microsoft Investor Stock Quote"
    
                 With ActiveSheet.QueryTables.Add(Connection:= _
                 "FINDER;" & IQYFile, Destination:=Range("A1"))
    
                    .PostText = _
                       "QUOTE0=[""QUOTE0"",""Enter up to 20 symbols " & _
                       separated by spaces.""]"
    
                    .BackgroundQuery = True
                    .TablesOnlyFromHTML = True
                   .Refresh BackgroundQuery:=False
                    .SaveData = True
                 End With
           End Sub 


  2. Switch to Sheet4 in the workbook and run the Finder_Post_Query macro.

    Because this example uses dynamic parameters, you are prompted for the parameter values.


  3. In the Enter Parameter Value dialog box, type up to twenty valid stock symbols that are separated by a space. For example, type msft.


  4. Click OK.


The stock quotes for the stock symbols you entered are returned to a table in Sheet4.

For additional information, please see the following article in the Microsoft Knowledge Base:
Q157482 XL97: How to Create Web Query (.iqy) Files


REFERENCES

For more information about Web queries, click Contents And Index on the Help menu (or on the Balloon Help menu if you are using a version of the Macintosh operating system earlier than 8.0), click the Index button in Microsoft Excel Help, type the following text

web, queries
and then click Show Topics. Select the "Get data from a Web source" topic, and click Go To. If you are unable to find the information you need, ask the Office Assistant.

Additional query words: XL98

Keywords : kbprg kbdta xlvbahowto xlvbainfo xlquery
Version : MACINTOSH:98
Platform : MACINTOSH
Issue type : kbhowto


Last Reviewed: November 9, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.