Megosztás a következőn keresztül:


How to: Populate Documents with Data from Services

Applies to

The information in this topic applies only to the specified Visual Studio Tools for Office projects and versions of Microsoft Office.

Project type

  • Document-level projects

Microsoft Office version

  • Excel 2007

  • Excel 2003

  • Word 2007

  • Word 2003

For more information, see Features Available by Application and Project Type.

Data access works the same way in document-level Visual Studio Tools for Office projects 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.

Starting in Visual Studio 2008 Service Pack 1 (SP1), you can also access data in application-level projects programmatically at run time.

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 Web 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.

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 Data to Controls in Office Solutions

Data Sources Overview

Displaying Data Overview

Change History

Date

History

Reason

July 2008

Added SP1 information.

SP1 feature change.