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 2007 and Excel 2010; Word 2007 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.
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
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.
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.
In your code, create an instance of the Web service class that you connected to in step 1.
If there are properties that are required for communication with the Web service, create instances of those properties.
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.
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.
See Also
Tasks
How to: Populate Worksheets with Data from a Database
How to: Populate Documents with Data from Objects
How to: Populate Documents with Data from a Database
How to: Update a Data Source with Data from a Host Control
Concepts
Binding Windows Forms Controls to Data in Visual Studio