Printer Friendly Version      Send     
Click to Rate and Give Feedback
Related Articles
ADO.NET Data Services provide Web-accessible endpoints that allow you to filter, sort, shape, and page data without having to build that functionality yourself.

By Shawn Wildermuth (September 2008)
Here John Papa demonstrates how to build a Silverlight 2 user interface that communicates through WCF to interact with business entities and a database.

By John Papa (September 2008)
Learn how to enable an auditing solution for Windows SharePoint Services (WSS) 3.0 with customized control pages in Microsoft Office SharePoint Server (MOSS) 2007.

By Ted Pattison (September 2008)
This month Dino builds a service layer that authenticates users of Silverlight 2 and ASP.NET AJAX services to prevent illegal access to sensitive back-end services.

By Dino Esposito (September 2008)
More ...
Articles by this Author
SQL Server 2000 Meta Data Services is a repository technology that stores and manages metadata for SQL Server. Instead of building database schemas over and over, Meta Data Services allows you to freeze an entire schema for use in other projects. You can also use these schemas for training, testing, or debugging. In this article, the authors will review the various components of Meta Data Services and show how it can be programmed using a Visual Basic client, XML, and XSLT. They will also show you how to manage and manipulate your metadata by generating a simple database schema using a SQL Server repository.

By Alok Mehta and Ricardo Rodriguez (May 2003)
Application service providers often must send information to clients automatically rather than on-demand. For example, a manufacturer may want to know each day how many of their products were sold by a retail chain. While SQL Server is ideal for maintaining this type of database, you have to write scripts to get the data out in a client-friendly format. Here you will see how you can use Data Transformation Services (DTS), a powerful tool in SQL Server, to automate the retrieval and formatting of data from SQL Server 2000 and make the process of pushing data to your users a lot easier.

By Alok Mehta and Daniel Williams (August 2002)
Using Microsoft technologies, you can insert, edit, query, and delete database entries using any e-mail client such as Hotmail, Outlook, Yahoo, or even WAP phone. While e-mail is certainly a powerful and widely used tool, it is usually not integrated with an application for performing any tasks other than sending reminders. The application scenario described here, an e-mail-based SQL update program, uses a simple data model; however, this solution will apply to any data model that you are working with. It will also eliminate the need for complex n-tier Internet applications and serves as a low maintenance solution for providing data access.

By Alok Mehta and Daniel Williams (January 2002)
More ...
Popular Articles
Microsoft Robotics Studio is not just for playing with robots. It also allows you to build service-based applications for a wide range of hardware devices.

By Sara Morgan (June 2008)
Learn how to automate custom SharePoint application deployments, use the SharePoint API, and avoid the hassle of custom site definitions.

By E. Wilansky, P. Olszewski, and R. Sneddon (May 2008)
Here we present techniques for programmatic and declarative data binding and display with Windows Presentation Foundation.

By Josh Smith (July 2008)
See how routed events and routed commands in Windows Presentation Foundation form the basis for communication between the parts of your UI.

By Brian Noyes (September 2008)
More ...
Read the Blog
Silverlight 2 features a rich and robust control model that is the basis for the controls included in the platform and for third-party control packages. You can also use this control model to build controls of your own. In the August 2008 issue of MSDN Magazine, Jeff Prosise describes how to ...
Read more!
In the August 2008 issue of MSDN Magazine, Matt Milner covers several topics regarding development with Windows Workflow Foundation, some that are intended to address specific reader questions, such as how to safely share a persistence database ...
Read more!
LINQ is a powerful tool enabling quick filtering data based on a standard query language. It can tear through a structured set of data using a simple and straightforward syntax. In the August 2008 issue of MSDN Magazine, Jared Parsons demonstrates a ...
Read more!
One of the very cool features of Silverlight that hasn't gotten the attention it deserves is the InkPresenter control, which enables Internet users to draw directly onto Silverlight applications from their browser. In the August 2008 issue of MSDN Magazine, Julia Lerman builds ...
Read more!
One of the neat things about XAML is that you can not only declare your objects using an XML syntax, but that you can define transformations to rotate, move, and skew your objects. In the August 2008 issue of MSDN Magazine, in an article adapted from his upcoming book Introducing Microsoft Silverlight ...
Read more!
Microsoft has a long history of introducing new features to shipped products, often under the banner of Power Toys or Power Tools. In the August 2008 issue of MSDN Magazine, Brian Randell takes you on a tour of some useful tools for ...
Read more!
More ...
Excel
Integrate Far-Flung Data into Your Spreadsheets with the Help of Web Services
Alok Mehta

This article discusses:
  • How you can integrate data from several Web services using Excel and VBA
  • The features of the Office Web Services Toolkit
  • How you can manipulate your data in Excel and use it to create reports
  • A sample application to get you started
This article uses the following technologies:
Office 2003, Web Services, Visual Basic
Microsoft® Office 2003 contains powerful XML-based functionality, including Web services integration. For example, using the Web services capabilities of Office 2003, developers can create a variety of solutions that help users increase their productivity on eBay. Web services offered by eBay allow you to use Excel to manage dynamic listing data, such as pricing and bids, and update this data automatically as changes occur. Solutions could also enable users to list multiple items on eBay simultaneously or to create a Web site that displays their eBay item listings.
This is all made possible with the Office 2003 Web Services Toolkit, which minimizes the programming required to integrate data returned from Web services with Visual Basic® for Applications (VBA). Office 2003 and Web services together offer a platform for building robust solutions to manage useful applications. You can download this tool from Microsoft Office 2003 Web Services Toolkit 2.01.
Among the applications within Office, Excel 2003 in particular allows developers to dynamically integrate the data provided by Web services. It also allows you to take advantage of the latest capabilities in Office 2003 to customize list views, graphs, and charts, and to catalog bulk items online or offline. The Web service integration within Excel 2003 can also provide ad hoc data analysis in the familiar environment of Excel, such as statistical analysis of the data extracted from the Web service.
This article describes how the Office 2003 Web Services Toolkit provides an API that allows you to select the Web services you want and create proxy classes for your VBA. The tool is accessible from the Web Service References command on the Tools menu in the Office Visual Basic Editor after the toolkit has been installed. By using the Office Visual Basic Editor, programmers can now set references to XML Web services just as they would any intrinsic type library. The VBA proxy classes generated by the tool allow both early binding and IntelliSense®, which makes consuming XML Web services easier and more intuitive, and virtually eliminates syntax errors.

The Toolkit
The Office 2003 Web Services Toolkit allows you to quickly access and use Web services with VBA from Office 2003 applications. It allows you to use a GUI to either search for Web services from any Universal Description Discovery and Integration (UDDI) registry or to specify a particular Web service by providing a URL to the file that contains the Web service description. The target file can be in Web Services Description Language (WSDL) format or the Visual Studio® Discovery (VSDisco) format, which is still supported even though it's obsolete. In case the description file contains information on multiple Web services, the GUI allows the user to select either some or all of the Web services for use within their current document.
The tool then generates the code for classes that access the Web services using the Microsoft SOAP client object. These generated classes will have methods corresponding to the operations supported by the Web services. The user can write simple VBA scripts without having to know that the objects she is using are really proxies to Web services. The toolkit supports Office SOAP 3.0 and requires MSXML 3.0.
The Office 2003 Web Services Toolkit also includes the Web Services References Tool, which allows you to locate Web services. The best part of this toolkit is that it automatically generates code for you. I'll discuss the automatic code generation listings shortly.

An Academic Scenario
To illustrate the power of using ASP.NET Web services in VBA, I have developed a simple example that includes both a sample Web service as well as a spreadsheet that uses it. The purpose of the Web service is to retrieve students' grades for a given professor and semester. This example uses a database with a simple data model (see Figure 1). The general idea is as follows: a professor teaches a course with many students in a given semester. Students are graded on quizzes, homework, a mid-term exam, class participation, and a final exam. Many times, professors want to analyze student performance in a particular semester, looking at a student's average grades and other statistics. By exposing this information through a Web service, professors could consume that data in Excel 2003 and use it to calculate a variety of interesting statistics.
Figure 1 Student Grades Data Model 
The sample is divided into the following three parts: the server-based ASP.NET Web service, called Get_Grades; the client-side VBA in a spreadsheet, called Client.XLS, which was developed in Excel 2003; and the toolkit code that runs on the client side. This code is automatically generated by the toolkit and provides a proxy to the ASP.NET Web service by wrapping calls to it.

ASP.NET Web Services Server Setup
To create the Get_Grades Web service, you first need to create a Web service in ASP.NET and add the code from Figure 2. Start Visual Studio .NET and open a new ASP.NET Web service project. Name the Web service project Get_Grades. If your machine has IIS installed, ASP.NET will automatically create the Web service for it. In addition, ASP.NET creates several files. Among the most important is Service1.asmx.vb, which will contain the public method Get_Grades available via the Web services. Simply cut and paste the source code in Figure 2 into Service1.asmx.vb. I'll describe the Web service source code in the next section. For now, let's work on building the solution.

VBA Client Setup
To set up Client.XLS on the developer's machine, copy it from the code download to any folder you prefer. In Excel, use the Visual Basic Editor to look at the code attached to the XLS file. You should see cmdGetDataFromWebService_Click within the WebServiceClient Sheet object. The code for this is shown in Figure 3. The class module, clsws_Get_Grades, is shown in Figure 4.
Assuming that you have installed the toolkit, going to Tools | Web References in the editor should show you the screen that you can see in Figure 5.
Figure 5 Office 2003 Web Services Toolkit in Excel 
Open the Office Visual Basic Editor in any Office application that supports VBA. Set a reference to the Microsoft Soap Type Library (MSSOAP3.DLL) from the References dialog box (located in the Tools menu). Then, on the Tools menu, choose the Web Service References option.
One of the important items in the toolkit is a Web service search, which lets you search against a UDDI Business Registry using either keywords or a business name. (We will look at UDDI later.) The Web Service URL feature lets you perform an XML Web service search (including wildcards) using a specific URL to a WSDL or Visual Studio .NET Discovery (.vsdisco) file. The Search Results page then allows you to select available XML Web services for which you want the tool to create proxy classes. Finally, the test button allows you to test an XML Web method you select from Search Results (if the XML Web service administrator has provided a corresponding .asmx test harness page).
Once you select one or more Web services for which you want the tool to create proxy classes and click the Add button, the toolkit will create one class module (.cls) file for each selected Web service. Each .cls file will begin with the prefix clsws_ followed by the Web service's name. For instance, if I add a Web service to my project with the name Get_Grades, the class name will be clsws_Get_Grades.
Now, let's go back to the code for the Web service itself. Shown in Figure 2, this code exposes a public Web method called Get_Grades that accepts two parameters: nProfessorID and lngCourseID. Its purpose is to return grades of a particular professor and course in the form of an XML string. Figure 6 shows the Get Grades button.
Figure 6 Get Grades Button 
The example Web service uses the following four .NET namespaces: System, System.Data, System.SqlClient, and System.WebServices. You can get more information about these namespaces in the MSDN Library.
Since this system is designed for professors only, the bValidUser function was created to validate the professor ID that is being passed to the Web service. Likewise, each semester is identified using a semester ID. The code on the client sets and passes these IDs to the Web service to extract the relevant data from the database so that an analysis can be performed in the client Excel file or application. The bValidUser function can be enhanced to include more complex validation and verification logic by implementing signed security tokens such as X.509 and Kerberos tickets as explained in Security in a Web Services World: A Proposed Architecture and Roadmap. You can also enhance bValidateUser to pull data from your own database to map the professor and course IDs. However, for the purposes of this example, I have simply hardcoded the IDs to be 1 so we can focus on power of Web services and Excel.
I made use of ADO.NET DataSets and their connection properties to extract the data from the SQL Server database. GradesConn is the SQL Server connection that defines the connection to the Grades database. GradesDataSet is populated from the database, and the DataSet is then converted to an XML string and is ultimately returned using the GradesDataSet.GetXml property.
Figure 3 is the client-side VBA code. The bulk of the code is contained in the cmdGetDataFromWebService_Click event handler. The purpose of this routine is to get the data in the form of an XML string from the Web service and parse it into the specified cells. The data is retrieved by calling objGrades.wsm_Get_Grades(Professor_ID, Course_ID). Recall that Professor_ID and Course_ID are constants and are assumed to be provided by the IT department of the college. Once the XML string is returned from the Web service, it is saved to disk so you can deserialize the data into a DOM now or at a later date.
Now that you have a valid XML file, called c:\Grades.XML, you can iterate through all of its nodes and their children. As I iterate through each of the children, I can extract the values and field names using oRoot.childNodes.Item(intI).childNodes.Item(intJ).Text and oRoot.childNodes.Item(intI).childNodes.Item(intJ).baseName, respectively. The spreadsheet columns are then filled using ActiveSheet.Cells(intI + intOffset, strColumn).Value, where the row in the active sheet is set by intI + intOffset and strColumn is based on strFieldName. Once the code in Figure 3 is executed, you should see results, as shown in Figure 7.
Figure 7 Data Retrieved from Web Services in Excel 2003 
Now that we have extracted the data from the server, let's explore Client.XLS. Columns A through G are filled by the VBA code I just discussed. Column I presents the automatically calculated, weighted average of the assessment. It represents the final grade that the professor wants to see.
Since Excel provides an excellent utility for creating statistical functions and charts, I decided to use those functions to convert the data extracted from the Web service into useful information. This information is contained in the Information tab of Client.XLS and is shown in Figure 8. These four useful sets of information are marked as A, B, C, and D. Part A contains simple mean, median, and mode. Part B contains frequency (within 20 points) of the final grades. Parts C and D are graphical representations of the frequency and student grades.
The code in Figure 4 is automatically created by the toolkit. It includes a VBA class representation of the Web service as defined by http://localhost/grades/WebService.asmx?wsdl. This code is created by the Web service component that comes with Excel 2003, and modifying it may result in unexpected behavior—so don't make any changes to it. The most important function in this class is wsm_Get_Grades, which is the proxy function created from http://localhost/grades/WebService.asmx?wsdl. The function wsm_Get_Grades is defined to return XML data.

Web Service Security
A discussion of Web services security is outside the scope of this article, but it is a very important issue. The MSDN Library hosts several articles that discuss security principles in Web services, such as Security in a Web Services World: A Proposed Architecture and Roadmap. Also, the Patterns & Practices team at Microsoft has published several articles on Web service security, such as Web Services Security. You should also consider issuing signed security tokens such as X.509 and Kerberos tickets to your VBA clients for securing your Web service.

Wrap-Up
In this article, I've demonstrated how to use a powerful and familiar tool, Excel, to access XML data from a Web service using VBA. A stateless Web service can wrap complex business logic in a black box that can then be called using Excel 2003 as a client.
Although I only discussed the Web service and client that are on the same machine, in real applications the Web services will often be hosted on a different server. You can certainly publish your Web services on your own enterprise Web servers and have your users use the Excel VBA client to access the Web service API. Additionally, you can register your Web services on UDDI where other businesses can know about your presence and the Web service APIs. UDDI is a comprehensive industry initiative enabling businesses to define their business, discover other businesses, and share information about how they interact in a global registry. UDDI is the building block that enables businesses to quickly, easily, and dynamically find and transact with one another via VBA. The UDDI standard takes advantage of W3C standards such as XML, HTTP, and DNS protocols to provide service description and discovery. UDDI also addresses cross-platform programming features. You can find a lot of useful information on UDDI at uddi.microsoft.com.

Alok Mehta is senior vice president and chief technology officer of AFS Technologies in Weston, Massachusetts. He recently completed his Ph.D. in Computer Science from Worcester Polytechnic Institute.

© 2008 Microsoft Corporation and CMP Media, LLC. All rights reserved; reproduction in part or in whole without permission is prohibited.
Page view tracker