Microsoft KB Archive/114214

= ACC2: How to Programmatically Embed or Link an Object in a Form =

Article ID: 114214

Article Last Modified on 7/8/2002

-

APPLIES TO


 * Microsoft Access 2.0 Standard Edition

-



This article was previously published under Q114214



Moderate: Requires basic macro, coding, and interoperability skills.



SUMMARY
This article describes how to programmatically embed or link an object in an OLE object field on a form using the object frame properties in Microsoft Access version 2.0.



MORE INFORMATION
You can set the object frame Action property at run time to perform a number of operations on an object frame. These operations include the ability to embed and link objects in an object frame, as well as other operations for programmatic access to OLE functionality.

There are other object frame properties that need to be set as prerequisites to setting the Action property. The properties that need to be set are determined by the type of OLE object you are working with and the type of action, using the Action property, you want to perform.

NOTE: The constants for use by the Action property are defined in the CONSTANT.TXT file, which is included with Microsoft Access. To use the constants in your code, you must either declare them yourself or copy them from the CONSTANT.TXT file and paste them into the Declarations section of your Access Basic module.

Linking an OLE Object
To link an object in an object frame on a form, first set the following properties:


 * OLETypeAllowed: Set to OLE_LINKED to indicate the OLE field will contain a linked object.
 * SourceDoc: Set to the path and filename of the file to be linked.
 * SourceItem: Indicates the data in the source document to link to. This could be a cell or cell range in a Microsoft Excel spreadsheet, or a bookmark in a Microsoft Word for Windows document. This property setting is optional.

Once you set these properties, you can set the Action property to OLE_CREATE_LINK to link the object in the object frame.

The following sample code demonstrates how to programmatically link a range of cells from a Microsoft Excel spreadsheet to an OLE object field named Sales Totals bound to an object frame with the same name on a form:

  ' Specify what kind of object can appear in the field. [Sales Totals].OLETypeAllowed = OLE_LINKED

' Specify the file to be linked. [Sales Totals].SourceDoc = "C:\EXCEL\SALES.XLS"

' Specify the (optional) cell range to link to. [Sales Totals].SourceItem = "R1C1:R5C5"

' Create the linked object. [Sales Totals].Action = OLE_CREATE_LINK

Embedding an OLE Object
To embed an object in an object frame on a form, first set the following properties:


 * OLETypeAllowed: Set to OLE_EMBEDDED to indicate the OLE field will contain an embedded object.
 * SourceDoc: Set to the path and filename of the file containing the information to embed.

Once you set these properties, you can set the Action property to OLE_CREATE_EMBED to create an embedded object with a copy of the information in the source document file.

The following sample code demonstrates how to embed a Word for Windows document in an OLE object field named Word Documents bound to an object frame with the same name on a form:

  ' Specify what kind of object can appear in the field. [Word Documents].OLETypeAllowed = OLE_EMBEDDED

' Specify the file containing the information to embed. [Word Documents].SourceDoc = "C:\WINWORD\JONES.DOC"

' Create the embedded object. [Word Documents].Action = OLE_CREATE_EMBED

Creating an Empty Embedded Object
To create an empty embedded object in an object frame, first set the following properties:


 * Class: Set to the type of object you want to create. For a Microsoft Excel worksheet, use Excel.Sheet. For a Word for Windows document, use Word.Document.
 * OLETypeAllowed: Set to OLE_EMBEDDED to indicate the OLE field will contain an embedded object.

Once you set these properties, you can set the Action property to OLE_CREATE_EMBED to create an empty embedded object of the type specified in the Class property. Then, set the Action property again to invoke the application for that object.

The following sample code demonstrates how to programmatically create new Word for Windows documents in an OLE object field named Word Documents bound to an object frame with the same name on a form:

  ' Specify what kind of object can appear in the field. [Word Documents].Class = "Word.Document"

' Specify what kind of object can appear in the field. [Word Documents].OLETypeAllowed = OLE_EMBEDDED

' Create the embedded object. [Word Documents].Action = OLE_CREATE_EMBED

' Invoke Word for Windows to edit the empty embedded object. [Word Documents].Action = OLE_ACTIVATE

