Microsoft Office Online
Sign in to My Office Online (What's this?) | Sign in

 
 
Microsoft Office Excel
Search
Search
 
Check for updates: (c) Microsoft
Office downloads
 
 
 
Warning: You are viewing this page with an unsupported Web browser. This Web site works best with Microsoft Internet Explorer 6.0 or later, Firefox 1.5, or Netscape Navigator 8.0 or later. Learn more about supported browsers.

Email this linkEmail this link Printer-Friendly VersionPrinter-Friendly Version Bookmark and ShareShare
Turn around expense reports in a snap with Excel 2007 and XML
 

Two words that may make you run for cover: expense reports. You dread them, you defer them, and you despise them, especially when Accounting returns them for minute corrections.

Put those fears aside and learn to positively love expense reports. Impossible, you say? Not if you team up with Microsoft Office Excel 2007 and Extensible Markup Language (XML) to turn those expense reports around in no time flat. This article explains how you can automate expense reporting by using XML data in an Excel worksheet. We even provide a sample expense report, sample XML schema and data, and sample code to get you started.

In this article


Expense reports: the old-fashioned and new-fashioned way

Are you handling your expense reports by printing an Excel template, writing in your expenses by hand, and then turning it in with crumpled receipts and crossed fingers? I sure hope not. But even if your Information Technology (IT) department has expense report automation at the bottom of its backlog, don't despair. You can quickly push expense report automation up that stack of requested applications.

How, you ask? Two words that may make you leap for joy: smart client. In a traditional client/server application, data is stored on a network server database (the back end), and forms are displayed on a client computer (the front end). A smart client adds built-in support for XML, leverages the full power of a personal computer, offers a familiar user interface, and has a rich set of features that you can use to analyze, publish, report, and share data. Sounds like we just defined Excel, not to mention the Microsoft Office System.

Most users are already familiar with entering data into Excel. Besides, an Excel-based form is a natural for expense reports. You can take advantage of formulas (formula: A sequence of values, cell references, names, functions, or operators in a cell that together produce a new value. A formula always begins with an equal sign (=).), Excel tables, comments, data validation (data validation: An Excel feature that you can use to define restrictions on what data can or should be entered in a cell, and to display messages that prompt users for correct entries and notify users about incorrect entries.), cell and worksheet protection (protect: To make settings for a worksheet or workbook that prevent users from viewing or gaining access to the specified worksheet or workbook elements.), and even smart tags (smart tags: Data recognized and labeled as a particular type. For example, a person's name or the name of a recent Microsoft Outlook e-mail message recipient is a type of data that can be recognized and labeled with a smart tag.), to name a few features. And then, of course, there's XML: the open, industry standard that makes it easy to define, transmit, validate, and interpret structured data between applications and organizations.

Now toss the custom XML Schema (XML Schema: A formal specification, written in XML, that defines the structure of an XML document, including element names and rich data types, which elements can appear in combination, and which attributes are available for each element.) feature into the mix. This makes it possible to map an XML schema file (.xsd) to specific cells in a worksheet, save mapped cell data from your worksheet into an XML data file, or import an XML data file and bind the data to these mapped cells. You can think of Excel as an XML data file generator, a powerful front end to new or even earlier applications. In many cases, you won't even need to write code, either Microsoft Visual Basic for Applications (VBA) or Extensible Stylesheet Language Transformation (XSLT). If you do need code, you don't need much.

By combining the time-tested features of Excel with the XML schema-mapping features in Excel, you can fill out that expense report and submit it faster than you can say "Show me the money!" IT folks take notice: Custom XML schemas and XML data make it simple to retrofit an existing front end and connect with back-end databases. Your custom applications don't have to be continually redesigned from the ground up.

Top of Page Top of Page

How expense reports get done from the top down

The following diagram illustrates a typical workflow for expense report processing. This workflow uses Excel as a smart client on the front end and XML to communicate with a back-end database on a server.

Expense report process workflow

Callout 1 You fill out the expense report by using an Excel worksheet that has been mapped to a custom XML schema and save the data as an XML data file.

Callout 2 You submit the expense report data in the XML data file to the back-end database.

Callout 3  Accounting scrupulously checks the expense report for accuracy and adherence to internal business rules and spending limits.

Callout 4  If Accounting approves, you get a gold star (and a reimbursement check).

Callout 5  If Accounting does not approve (that fancy restaurant didn't pass muster), they send an e-mail message back to you telling you to adjust the expense report and resubmit it.

Let's look at the "Fill out expense report" step more closely.

Filling out the expense report

Callout 1 First, an IT developer creates a custom XML schema, defined in an XML schema file, and maps the appropriate cells in the Excel worksheet. Note that this is done only once. In practice, users obtain the expense report with the schema already mapped, often by downloading it from an internal Web site.

Callout 2 Then, users enter identification information and line-by-line details into an Excel worksheet that looks just like an expense report.

Callout 3  Finally, after completing the expense report, users save the data to an XML file.

Take a stroll through the process

Got motivation? Then put your walking shoes on and learn how to use the sample Excel workbook and XML schema file, step by step.

First, you'll add the schema to the Excel workbook and map specific data elements to corresponding cells in the expense report worksheet. Then, you can practice typing data into the expense report, and save the data to an XML file for further processing. Finally, you'll learn how you can submit an expense report and how to revise it, if necessary.

Examine ExpenseReport.xlsx

Now, let's look at a real Excel workbook (.xlsx) just to show we're not pulling your leg. You can use this workbook as a model for designing your own expense report workbook.

To download the workbook, do the following:

  1. Click the link Expense Report Template.
  2. In the File Download dialog box, click Save.

     Note   Do not click Open because doing so will display a warning message and open a read-only version of the workbook.

  3. In the Save As dialog box, click Save.
  4. When the file download is complete, click Open.
  5. Save the file as ExpenseReport.xlsx.

The expense report is an Excel worksheet that takes advantage of built-in features of Excel to streamline filling out an expense report.

The expense report template

Callout 1  Use comments to provide context-sensitive assistance for each cell.

Callout 2  Link users to an internal Web site for detailed information about how to use the expense report.

Callout 3  Visually highlight cells that contain formulas.

Callout 4  Lock cells that contain formulas, to protect them from users who are accident-prone.

Callout 5  Locally validate data, such as special codes.

In your application, make sure that IT developers protect the entire workbook and create a password before users obtain the workbook, so no one jimmies those locked cells.

Savvy IT developers can even add smart tags to the Excel template and turn a smart client into a bona-fide genius. For example, you can use a smart tag to pre-load the expense report with user information, validate cost center codes from an external database, or link to Web pages for detailed information about a cell.

Examine ExpenseReportSchema.xsd

The following XML schema file defines the data that you want to capture in the expense report and send to Accounting (the folks with the white shirts and green eyeshades). Usually, IT developers create this schema because they understand best what data needs to be defined and captured.

ShowExpenseReportSchema.xsd

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:expenseReport"
 targetNamespace="urn:expenseReport" elementFormDefault="qualified">
 <xsd:element name="Root">
  <xsd:complexType mixed="false">
  <xsd:sequence minOccurs="0" maxOccurs="1">
  <xsd:element minOccurs="1" maxOccurs="1" name="Meta">
   <xsd:complexType mixed="false">
   <xsd:all>
   <xsd:element minOccurs="1" maxOccurs="1" type="xsd:string" name="Name"/>
   <xsd:element minOccurs="1" maxOccurs="1" type="xsd:string" name="Email"/>
   <xsd:element minOccurs="1" maxOccurs="1" type="xsd:string" name="EmployeeNumber"/>
   <xsd:element minOccurs="1" maxOccurs="1" type="xsd:string" name="CompanyCode"/>
   <xsd:element minOccurs="1" maxOccurs="1" type="xsd:string" name="CostCenter"/>
   <xsd:element minOccurs="1" maxOccurs="1" type="xsd:date" name="StartDate"/>
   <xsd:element minOccurs="1" maxOccurs="1" type="xsd:date" name="EndDate"/>
   <xsd:element minOccurs="1" maxOccurs="1" type="xsd:string" name="Purpose"/>
   </xsd:all>
   </xsd:complexType>
  </xsd:element>
  <xsd:element minOccurs="1" maxOccurs="1" name="Summary">
   <xsd:complexType mixed="false">
   <xsd:all>
    <xsd:element type="xsd:decimal" name="TravelTotal"/>
    <xsd:element type="xsd:decimal" name="MealsTotal"/>
    <xsd:element type="xsd:decimal" name="ConferenceTotal"/>
   </xsd:all>
   </xsd:complexType>
  </xsd:element>
  <xsd:element minOccurs="0" maxOccurs="unbounded" name="ExpenseItem">
   <xsd:complexType mixed="false">
    <xsd:sequence minOccurs="0" maxOccurs="1">
     <xsd:element type="xsd:date" name="Date"/>
     <xsd:element type="xsd:string" name="Description"/>
     <xsd:element type="xsd:integer" name="Miles"/>
     <xsd:element type="xsd:decimal" name="Mileage"/>
     <xsd:element type="xsd:decimal" name="AirFare"/>
     <xsd:element type="xsd:decimal" name="Other"/>
     <xsd:element type="xsd:decimal" name="Meals"/>
     <xsd:element type="xsd:decimal" name="Conference"/>
     <xsd:element type="xsd:decimal" name="Misc"/>
     <xsd:element type="xsd:string" name="MiscCode"/>
     <xsd:element type="xsd:decimal" name="Amount"/>
    </xsd:sequence>
   </xsd:complexType>
  </xsd:element>
  </xsd:sequence>
  </xsd:complexType>
 </xsd:element>
 </xsd:schema>

For detailed information about how to create an XML schema, see XML Schema Part 0: Primer, published by the World Wide Web Consortium (W3C).

Add the sample schema to the workbook

  1. Save the sample schema to a file called, ExpenseReportSchema.xsd.
  2. Open ExpenseReport.xlsx.
  3. If the Developer tab is not available, do the following to display it:
    1. Click the Microsoft Office Button Button image, and then click Excel Options.
    2. In the Popular category, under Top options for working with Excel, select the Show Developer tab in the Ribbon check box, and then click OK.

       Note   The Ribbon is a component of the Microsoft Office Fluent user interface.

  4. On the Developer tab, in the XML group, click Source.

    XML group

    The XML Source task pane is displayed.

  5. Click XML Maps.
  6. Click Add.
  7. In the Look in list, click the drive, folder, or intranet location that contains ExpenseReport.xsd.
  8. Click ExpenseReport.xsd, and then click Open.
  9. Click OK to close the XML Maps dialog box.

The XML map is displayed in the XML Source task pane. The task pane displays a simple hierarchical view of the XML schema that makes it easy to view and map elements to cells.

Map the nonrepeating data to single-mapped cells

Nonrepeating data, such as information about the user or the purpose of the trip, is mapped to single cells. In an Excel worksheet, cells that contain nonrepeating data are called single-mapped cells.

  1. On the Developer tab, in the XML group, click Source.

    XML group

    The XML Source task pane is displayed.

  2. For each of the elements — user information (under ns1:Meta) and summary data (under ns1:Summary) — select the elements that you want in the XML Source task pane, and drag them to their corresponding cells.
    Drag and drop… Onto cell…
    ns1:Name C3
    ns1:Email C4
    ns1:EmployeeNumber C5
    ns1:CompanyCode C6
    ns1:CostCenter I3
    ns1:StartDate I4
    ns1:EndDate I5
    ns1:Purpose C8
    ns1:TravelTotal L20
    ns1:MealsTotal H20
    ns1:ConferenceTotal I20

    By default, a single-mapped cell is visually identified with a blue border.

    In general, when you drag a nonrepeating XML element onto the worksheet, you can choose to include the element name as a heading above or just to the left of the single-mapped cell. In the expense report, the existing cell value already has a heading or label.

Map the repeating data to an XML table

Repeating data elements, such as the line item information, are mapped to rows of data. In an Excel worksheet, rows that contain repeating data are XML tables.

  1. Select the ns1:ExpenseItem element in the XML Source task pane, and drag it to the B10 cell on the worksheet.
  2. To clear the default Excel table formatting, on the Design tab, in the Table Styles group, click the More arrow in the bottom right-hand corner, and then click Clear at the bottom of the Gallery window.

Congratulations! You just mapped all the line item columns in a single step, and you also created an XML table. An XML table has the added advantage of automatically expanding when users add new rows to the table in the worksheet.

 Note   Repeating data elements are mapped together into an XML table when the XML Map property Automatically Merge Elements When Mapping is selected (this is the default value). To modify this and other XML map properties, right-click on the XML table, click XML, and then click XML Map Properties.

Enter data into the Excel expense report

Users of the expense report can now organize their crumpled receipts and type the data into the spreadsheet. Still got motivation? Type the following sample data into the expense report and feel the love.

Typical example of expense report with data filled out

Submit the expense report data

After you submit the XML data file to a pickup folder, the data can be processed in many different ways, including the system that your enterprise uses to handle expense reports, of which there can be quite a variety. For example, the XML data file could be sent to the Accounting department, perhaps to a dedicated e-mail account that retrieves the XML data and stores it in an equally dedicated folder. The XML file could also be imported into any number of databases, including Microsoft Office Access, Microsoft SQL Server, and third party databases. Or, you may have an Office SharePoint Server custom workflow operation that uses Microsoft Office InfoPath and Forms Server. There are many possibilities, but this attests to the remarkable flexibility of XML.

Here are two ways you can submit the data, one without code, and the other with code.

Save the expense report data as an XML data file without code

When you manually type (or import) data into a mapped workbook, that data becomes part of the workbook when it is saved. But it is not automatically saved as an XML data file. To create an XML data file, do the following:

  1. On the Developer tab, in the XML group, click Export.

    XML group in Ribbon

     Note   If a small Export XML dialog box appears, click the XML map that you want to use, and then click OK.

    The Export XML dialog box will only appear if an XML table is not selected and if the workbook contains more than one XML map.

  2. If the Export XML dialog box appears, click the XML map that you want to use and then click OK.

    The Export XML dialog box appears only if an XML table is not selected and the workbook contains more than one XML map.

  3. In the File name box, type ExpenseReportData.xml for the XML data file.
  4. Click Export.

If all is well, your data should look like the following file.

ShowExpenseReportData.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns1:Root xmlns:ns1="urn:expenseReport">
 <ns1:Meta>
  <ns1:Name>Carol Philips</ns1:Name>
  <ns1:Email>CaPhi</ns1:Email>
  <ns1:EmployeeNumber>36833</ns1:EmployeeNumber>
  <ns1:CompanyCode>425</ns1:CompanyCode>
  <ns1:CostCenter>D98</ns1:CostCenter>
  <ns1:StartDate>2004-02-04</ns1:StartDate>
  <ns1:EndDate>2004-02-07</ns1:EndDate>
  <ns1:Purpose>Attend business conference for training and hobnob with customers.</ns1:Purpose>
 </ns1:Meta>
 <ns1:Summary>
  <ns1:TravelTotal>2414.41</ns1:TravelTotal>
  <ns1:MealsTotal>91.53</ns1:MealsTotal>
  <ns1:ConferenceTotal>599</ns1:ConferenceTotal>
 </ns1:Summary>
 <ns1:ExpenseItem>
  <ns1:Date>2004-02-04</ns1:Date>
  <ns1:Description>Flight</ns1:Description>
  <ns1:Miles/>
  <ns1:Mileage>0</ns1:Mileage>
  <ns1:AirFare>659.78</ns1:AirFare>
  <ns1:Other/>
  <ns1:Meals/>
  <ns1:Conference/>
  <ns1:Misc/>
  <ns1:MiscCode/>
  <ns1:Amount>659.78</ns1:Amount>
 </ns1:ExpenseItem>
 <ns1:ExpenseItem>
  <ns1:Date>2004-02-04</ns1:Date>
  <ns1:Description>Tips &amp; Meals</ns1:Description>
  <ns1:Miles/>
  <ns1:Mileage>0</ns1:Mileage>
  <ns1:AirFare/>
  <ns1:Other>5.89</ns1:Other>
  <ns1:Meals>23.67</ns1:Meals>
  <ns1:Conference/>
  <ns1:Misc/>
  <ns1:MiscCode/>
  <ns1:Amount>29.56</ns1:Amount>
 </ns1:ExpenseItem>
 <ns1:ExpenseItem>
  <ns1:Date>2004-02-05</ns1:Date>
  <ns1:Description>Conf. Fee</ns1:Description>
  <ns1:Miles/>
  <ns1:Mileage>0</ns1:Mileage>
  <ns1:AirFare/>
  <ns1:Other/>
  <ns1:Meals/>
  <ns1:Conference>599</ns1:Conference>
  <ns1:Misc/>
  <ns1:MiscCode/>
  <ns1:Amount>599</ns1:Amount>
 </ns1:ExpenseItem>
 <ns1:ExpenseItem>
  <ns1:Date>2004-02-05</ns1:Date>
  <ns1:Description>Tips &amp; Meals</ns1:Description>
  <ns1:Miles/>
  <ns1:Mileage>0</ns1:Mileage>
  <ns1:AirFare/>
  <ns1:Other>7.56</ns1:Other>
  <ns1:Meals>67.86</ns1:Meals>
  <ns1:Conference/>
  <ns1:Misc/>
  <ns1:MiscCode/>
  <ns1:Amount>75.42</ns1:Amount>
 </ns1:ExpenseItem>
 <ns1:ExpenseItem>
  <ns1:Date>2004-02-06</ns1:Date>
  <ns1:Description>Schmoozing</ns1:Description>
  <ns1:Miles/>
  <ns1:Mileage>0</ns1:Mileage>
  <ns1:AirFare/>
  <ns1:Other/>
  <ns1:Meals/>
  <ns1:Conference/>
  <ns1:Misc>345</ns1:Misc>
  <ns1:MiscCode>D</ns1:MiscCode>
  <ns1:Amount>345</ns1:Amount>
 </ns1:ExpenseItem>
 <ns1:ExpenseItem>
  <ns1:Date>2004-02-07</ns1:Date>
  <ns1:Description>Sidetrip</ns1:Description>
  <ns1:Miles>185</ns1:Miles>
  <ns1:Mileage>59.2</ns1:Mileage>
  <ns1:AirFare/>
  <ns1:Other/>
  <ns1:Meals/>
  <ns1:Conference/>
  <ns1:Misc/>
  <ns1:MiscCode/>
  <ns1:Amount>59.2</ns1:Amount>
 </ns1:ExpenseItem>
 <ns1:ExpenseItem>
  <ns1:Date>2004-02-07</ns1:Date>
  <ns1:Description>Hotel</ns1:Description>
  <ns1:Miles/>
  <ns1:Mileage>0</ns1:Mileage>
  <ns1:AirFare/>
  <ns1:Other/>
  <ns1:Meals/>
  <ns1:Conference/>
  <ns1:Misc>389.78</ns1:Misc>
  <ns1:MiscCode>C</ns1:MiscCode>
  <ns1:Amount>389.78</ns1:Amount>
 </ns1:ExpenseItem>
 <ns1:ExpenseItem>
  <ns1:Date>2004-02-07</ns1:Date>
  <ns1:Description>Car Rental</ns1:Description>
  <ns1:Miles/>
  <ns1:Mileage>0</ns1:Mileage>
  <ns1:AirFare/>
  <ns1:Other>256.67</ns1:Other>
  <ns1:Meals/>
  <ns1:Conference/>
  <ns1:Misc/>
  <ns1:MiscCode/>
  <ns1:Amount>256.67</ns1:Amount>
 </ns1:ExpenseItem>
</ns1:Root>

Save the expense report data as an XML data file with code

With just a few lines of VBA code, you or your IT developer can add a Submit button that would export the data for you and send it to a pickup folder for subsequent processing and verification. Here's an example that creates a time-stamped XML data file and saves the workbook with the same timestamp so you and Accounting can keep track of each go round:

ShowVBA code example

Dim SbmtXMLFile, SaveAsFile, SbmtAnswer, SbmtMsg, _
    ConfirmMsg, SbmtTitle, SbmtPrompt, SbmtStyle, _
    CurrPath, PickupPath, TimeStamp

' Define path for workbook as current folder.
CurrPath = ActiveWorkbook.Path & "\"
    
' Enter a different path with a trailing slashmark for the
' XML file, or use the current folder.
PickupPath = CurrPath
        
' Define workbook and XML filenames with the same unique time stamp.
TimeStamp = Format(Date, "yyyy-mm-dd") & "-" & Format(Time, "hh-mm-ss")
SbmtXMLFile = PickupPath & Range("C5") & "-" & TimeStamp & ".xml"
SaveAsFile = CurrPath & Range("C5") & "-" & TimeStamp & ".xlsm"
    
' Define MsgBox variables.
SbmtPrompt = "Do you want to submit your expenses?"
SbmtTitle = "Expense Report Utility"
SbmtStyle = vbYesNo + vbInformation + vbDefaultButton2
SbmtMsg = "Expenses submitted and workbook saved."
        
' Confirm submit operation:
'   If Yes, submit expenses, save file, and close workbook.
'   If No, display a message.
SbmtAnswer = MsgBox(SbmtPrompt, SbmtStyle, SbmtTitle)
If SbmtAnswer = vbYes Then
    ActiveWorkbook.XmlMaps("Root_Map").Export URL:=SbmtXMLFile
    ActiveWorkbook.SaveAs SaveAsFile
    ConfirmMsg = MsgBox(SbmtMsg, vbOKOnly, SbmtTitle)
    ActiveWorkbook.Close SaveChanges:=False
Else
    ConfirmMsg = MsgBox("Try again later.", vbOKOnly, SbmtTitle)
End If

To add this code, do the following:

  1. In ExpenseReport.xlsx, on the Developer tab, in the Code group, click Macros.
  2. In the Macro name box, enter ExportXMLToFile, and then click Create.
  3. Copy and paste the code above into the ExportXMLToFile Sub procedure.
  4. Close the VBA window.
  5. On the Developer tab, in the Controls group, click Insert, and then under Form Controls, click Button Button image.
  6. Click the worksheet location where you want the upper-left corner of the button to appear.
  7. To assign the ExportXMLToFile macro to the button, in the Assign Macro dialog box, select ExportXMLToFile, and then click OK.
  8. To specify a label for the button, replace the default text with the text, "Submit", and then click a blank cell outside the button.
  9. To save the workbook as a macro-enabled workbook, Click the Microsoft Office Button Office button image, point to Save As, click Excel Macro-Enabled Workbook, and then save it to ExpenseReport.xlsm.
  10. To test the macro, click the button, and then follow the directions.

Revise the expense report data

If you need to make changes to your expense report later, because Accounting sent you an e-mail with corrections, you can revise it by opening the workbook, Expensereport.xlsm, making the necessary changes in the workbook, and then resubmitting the data. It's that easy.

The bottom line

Expense reports in your company are now a breeze. At that recent business conference in Chicago, you were a hero with your fellow road warriors. Why, just last week, the entire IT department delivered flowers to your office and personally thanked you for lightening their load. The other day, you courted an important customer with an expensive dinner and finally closed that big sale. This morning, even your boss patted you on the back, complimented you on that paisley tie, and said…

"Don't forget to fill out those expense reports."

advertisement