There are many reasons why you might want to use XML Web Services--for instance, once you get them set up, they're easy; they're cross-platform and cross-language; and they are (or will be) everywhere. For more information about Web services and some of the issues surrounding them, see the following articles and related discussions: "Is Your Company Ready for the Web Services Revolution?", "Get Ready for Web Services! (Oops! Wait!)", and "XML Part 2: Web Services."
To test integration functionality, I wanted to see "inventory results" from a remote Web service updated automatically within an Excel spreadsheet. Of course, there are many ways to accomplish automatic data updating within Excel, but this article explores how you can use Excel with Web Services, which further extends your data integration possibilities. It turns out that it's super easy, and I'm going to show you some of the highlights of how to do it in this article.
This example is very similar to the one that's included with the toolkit itself, but I wanted to use Microsoft Excel to consume the Java-based Web service that Michael Floyd wrote about. Mike used Sun's Java Web Services Developer Pack to create a Web service that accepts part numbers and returns the inventory count for the part number. It turns out that if you use all the pieces of the kit that Sun provides, a Web Service can be implemented using just a small amount of Java code. Read Mike's article for details.
The Office Web Services Toolkit
To use a Web service within any Office XP application (be it Word 2002, Excel 2002, Access 2002, FrontPage 2002, PowerPoint 2002, etc.), you have to reference the service with code. Fortunately, it's super easy, even if you don't really code much. The Microsoft Office Web Services Toolkit is a free, downloadable extension of Office XP that provides the bridge (I'm not sure if it's available for previous versions of Office). Because it requires VBA coding (as of this writing), it's not a toolkit that the casual Excel user is likely to use directly, but it's simple enough that even a modest understanding of VBA can get you retrieving Web services into spreadsheets in no time. All you have to do is create a function that calls the Web service and returns the data. The function can be written in fewer than four lines of code!
Creating a "Web Service" Function
The first step is to visit the Microsoft Office Web Services Toolkit download page. Download the toolkit and install it per the instructions. After you've downloaded and installed the Toolkit, open a new Excel worksheet, then hit Tools->Macro->VBA Editor. In the VBA code editor, you can create the code that consumes the Web service. Web services can be thought of as similar to COM objects on a remote server, and as with other COM objects, you need to reference them to be able to use them. The Web Services Toolkit creates a new menu item in the VBA environment that allows you to reference Web services. To use it, hit Tools->Web Service References. That brings up the screen shown in Figure 1, which allows you to search for Web services and bring them into your project as references automatically. Either provide the URL where you know the service resides, or search for it using the Web Service Search option. For this example, I entered the exact URL of the Web service Mike created for his article. I selected the MyInventory service and then clicked Add.
Figure 1: Referencing a Web service is as easy as entering a URL to browse and selecting an available service. (Click images to enlarge.)
When you add the reference to the Web service, the Web Services Reference tool adds a bunch of code and supporting elements to your project. Using the tool makes it easy because you don't have to worry about some of the specifics of communications. All you have to do is add code similar to the following example to your project (in a new or existing module), and you're retrieving Web services. Told you Web services were simple!
The following code defines a function that retrieves items from the service based upon the part number passed in sPart parameter.
Dim Part As New clsws_MyInventory
GetInventory = Part.wsm_checkInventory(sPart)
End Function
' The following code invokes the function for three parts,
' printing the results in the debug window
Sub runit ()
Debug.Print GetInventory("AAA")
Debug.Print GetInventory("BBB")
Debug.Print GetInventory("CCC")
End Sub
If you run the "runit" subroutine, the results are shown in the Immediate window, as shown in Figure 2. Bamma lamma! You're reading a Web service.
Figure 2: Once the Web service function is defined, you can call it as a standard VBA function.
Once you get the data that comes back from the Web service into the function, you can just use it like any other Office data. You could copy the data to records in an Access database, use it as a part of a Word document, or plug it into spreadsheets, for example.
Using the "Web Service" Function in Excel
To plug the VBA function calling the Web service into a worksheet cell or range in an Excel spreadsheet, position to the cell you want and hit Insert->Function, as shown in Figure 3.
Figure 3: Insert your Web service function just like any other Excel function.
In the Select a Category field, choose User Defined, and then choose your function that calls the Web Service, as shown in Figure 4.
Figure 4: You can select user-defined functions for insertion.
The next screen, shown in Figure 5, asks you to fill in the parameters for the function. For this example, I chose a cell that contains an item number, and I'll fill in the formula down in the next step.
Figure 5: Tell Excel where to get the parameters required for the function.
Click the OK button to insert the function that calls the Web service. The function will be called and the results put into the cell. If you copy the formula to adjacent cells (fill down or right), the inventory count of each part number will be pulled, as shown in Figure 6.
Figure 6: Filling the formula down results in it being called for each part listed in the A column.
You Can Go Crazy with This Stuff
Of course, this is a simplistic example, but you can use it as a basis for more complex and informative exchanges. Since you can pass data back and forth between functions, you could build update functionality right into the spreadsheet, if you dared. To do so, you could make the called function examine the incoming parameter and take action accordingly. The capability is there, but it's up to you to figure out what kind of system you'll design when the boss wants to be able to update the "bottom line" figure on his spreadsheet. Seriously, assuming proper security, users might prefer using familiar programs such as Excel to work with data in ways they're authorized to. It sure beats re-keying!
When you give the spreadsheet to others, they need to have all the required parts of the Office Web Services Toolkit in place to be able to run it correctly. To get those parts in place, either download them and install the Toolkit, or use the Visual Studio Installer or another installation package to create your own SETUP program that includes the necessary components. Instructions on how to create such a distribution are included in the Toolkit documentation.
As I'm keeping an eye on the burgeoning Web services marketplace, it seems that many of the "elite" thinkers have taken the basic concepts of Web service and extended them as far as they could. Who knows if the Web service architecture will "reshape the computing landscape," but one thing is for sure--they sure are handy for communicating!
Brian Singleton, formerly editor of Midrange Computing magazine, is a freelance author, trainer, and consultant. Over the past few years, Brian has developed a line of best-selling training videos, authored the best-selling The OS/400 and Microsoft Office Integration Handbook--Second Edition, and spoken at many popular seminars and conferences. He can be reached at www.bsing.com.
LATEST COMMENTS
MC Press Online