Mon, Jun
3 New Articles

Microsoft Computing: Including a Web Service in Excel--It's Crazy Easy!

  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

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.

Public Function GetInventory(sPart As String) As String
   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.

Brian Singleton
Brian Singleton is former editor of Midrange Computing. He has worked in the IBM midrange arena for many years, performing every job from backup operator to programmer to systems analyst to technology analyst for major corporations and IBM Business Partners. He also has an extensive background in the PC world. Brian also developed a line of bestselling Midrange Computing training videos, authored the bestselling i5/OS and Microsoft Office Integration Handbook, and has spoken at many popular seminars and conferences.

MC Press books written by Brian Singleton available now on the MC Press Bookstore.

i5/OS and Microsoft Office Integration Handbook i5/OS and Microsoft Office Integration Handbook
Harness the power of Microsoft Office while exploiting the iSeries database.
List Price $79.95

Now On Sale



Support MC Press Online

$0.00 Raised:

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: