Export (0) Print
Expand All

How to: Populate Documents with Data from Services

Data access works the same way in document-level projects for Microsoft Office as it does in Windows Forms projects. You use the same tools and code to bring the data into your solution, and you can even use Windows Forms controls to display the data. In addition, you can take advantage of controls called host controls, which are native objects in Microsoft Office Excel and Microsoft Office Word that have been enhanced with events and data binding capability. For more information, see Host Items and Host Controls Overview.

Applies to: The information in this topic applies to document-level projects for the following applications: Excel 2013 and Excel 2010; Word 2013 and Word 2010. For more information, see Features Available by Office Application and Project Type.

The following example shows how to add data-bound controls to documents at design time. For an example of how to add data-bound controls in application-level add-ins at run time, see Walkthrough: Binding to Data from a Service in an Application-Level Project.

link to video For a related video demonstration, see How Do I: Interact with Web Services from Microsoft Excel?.

To populate a document-level project with data from a Web service

  1. Open the Data Sources window and create a service data source for your project. For more information, see How to: Connect to Data in a Service.

  2. Drag the table or field you want from the Data Sources window to your document.

    A control is created on the document, a BindingSource is created that is bound to the object class in your project, and classes are generated for the service.

  3. In your code, create an instance of the Web service class that you connected to in step 1.

  4. If there are properties that are required for communication with the Web service, create instances of those properties.

  5. Create and send a data request using methods exposed by the Web service and any property instances you created in step 4.

    The methods that you use depend on what the Web service offers.

  6. Assign the data response from the Web service to the DataSource property of the BindingSource.

When you run the project, the controls display the first record in the data source. You can enable scrolling through the records by handling the currency events using the objects in the BindingSource.

Show:
© 2014 Microsoft