How to: Programmatically Cache a Data Source in an Office Document

 

You can programmatically add a data object to the data cache in a document by calling the StartCaching method of a host item, such as a Document, Workbook, or Worksheet. Remove a data object from the data cache by calling the StopCaching method of a host item.

The StartCaching method and the StopCaching method are both private, but they appear in IntelliSense.

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

When you use the StartCaching method to add a data object to the data cache, the data object does not need to be declared with the CachedAttribute attribute. However, the data object must meet certain requirements to be added to the data cache. For more information, see Caching Data.

To programmatically cache a data object

  1. Declare the data object at the class level, not inside a method. This example assumes that you are declaring a DataSet named dataSet1 that you want to cache programmatically.

        Public dataSet1 As DataSet
    

  2. Instantiate the data object, and then call the StartCaching method of the document or worksheet instance and pass in the name of the data object.

            dataSet1 = New DataSet()
    
            If Not (Me.IsCached("dataSet1")) Then
                Me.StartCaching("dataSet1")
            End If
    

To stop caching a data object

  1. Call the StopCaching method of the document or worksheet instance and pass in the name of the data object. This example assumes that you have a DataSet named dataSet1 that you want to stop caching.

            If (Me.IsCached("dataSet1")) Then
                Me.StopCaching("dataSet1")
            End If
    

    System_CAPS_ICON_note.jpg Note


    Do not call StopCaching from the event handler for the Shutdown event of a document or worksheet. By the time the Shutdown event is raised, it is too late to modify the data cache. For more information about the Shutdown event, see Events in Office Projects.

Caching Data
How to: Cache Data for Use Offline or on a Server
How to: Cache Data in a Password-Protected Document
Accessing Data in Documents on the Server
Saving Data

Show: