Share via


How to: Add OLE Objects to Applications

You can add OLE objects to tables, forms, and reports. For more information about adding OLE objects to reports, see How to: Add General Fields to Reports.

Adding OLE Objects to Tables

You can include Word documents in a table by creating General fields and adding the documents by linking or embedding them.

To add an OLE object to a table

  1. Open the table in the Table Designer.

  2. In the Table Designer, create a General field.

  3. Open a browse window for the table.

  4. Double-click the General field to open an editing window.

    Tip

    You can also use the MODIFY GENERAL command.

  5. On the Edit menu, click Insert Object.

To add an OLE object to a table programmatically

  • Use the APPEND GENERAL command.

For more information, see APPEND GENERAL Command.

You can use APPEND GENERAL to embed OLE objects or link to OLE objects created by applications such as Microsoft Excel and Word. These applications support both linking and embedding. However, some applications such as Microsoft Graph only support embedding. When you use the APPEND GENERAL command, you can import the OLE object from a file and insert it into a General field. If the field already contains an object, the new object replaces it.

Note

Unlike APPEND and APPEND BLANK, APPEND GENERAL does not add a new record to the table.

For example, suppose you have Microsoft Word files that you want to store in a Visual FoxPro table. If the table has a General field named WordDoc, you can embed the documents by using the following code:

CREATE TABLE oletable (name c(24), worddoc g)
CD GETDIR()

nFiles = ADIR(aWordFiles, "*.doc")
IF nFiles > 0
   FOR i = 1 to nFiles
      APPEND BLANK
      REPLACE Oletable.Name WITH aWordFiles(i,1)
      APPEND GENERAL WordDoc FROM aWordFiles(i,1)
   ENDFOR
ELSE
   MESSAGEBOX("No Word files found.")
ENDIF

Note

The preceding example looks only for files ending in .doc, the standard extension used by Word files. Because Microsoft Word and OLE are aware of this, the files are automatically associated with the Word server when you use APPEND GENERAL.

If you use a different extension than the one expected by the server, you must declare the class of the server, using the CLASS clause. For example, if you add the class for Word to the previous example, the code becomes:

APPEND GENERAL WordDoc FROM wordfiles(i,1) CLASS "Word.Document"

If you have files with common extensions (for example, .bmp) that other servers might use, you can use the CLASS clause to specify the particular server you want to use for those files. Alternatively, if you'd rather link than embed objects, use the LINK keyword, as in the following example:

APPEND GENERAL WordDoc FROM wordfiles(i,1) LINK CLASS "Word.Document"

In addition, you can replace data in an object by using the DATA keyword of APPEND GENERAL.

Adding OLE Objects to Forms

Using the Form Designer, you can add insertable OLE objects to forms with the OLE Container control. In addition, you can display OLE objects from General fields by using the OLE Bound control. When you add an OLE object to a form in either the OLE Container control or the OLE Bound control, you have more control over the opening and editing of the object.

You can determine whether the OLE object is opened or edited when the control gets the focus or when the user double-clicks the control by setting the AutoActivate property of an OLE bound or container control. The AutoVerbMenu property specifies whether the shortcut menu of the ActiveX control allows a user to open or edit the OLE object. To control access so that the OLE object can only be opened or edited programmatically with the DoVerb method, set AutoActivate to 0 - Manual and AutoVerbMenu to false (.F.).

To add an OLE object to a form

  1. In the Form Designer, add an OLE Container control to your form. The Insert Object dialog box opens.

  2. In the Insert Object dialog box, select Create New or Create from File.

  3. Choose the appropriate OLE object from the Object Type list.

You can also customize the Form Controls toolbar so that you can directly add specific OLE objects.

To add OLE objects to the Form Controls toolbar

  1. From the Tools menu, choose Options.

  2. In the Controls tab of the Options dialog box, choose ActiveX controls.

  3. In the Selected list, select the OLE objects and ActiveX controls you want to be available from the Form Controls toolbar.

  4. Choose Set as Default, and then choose OK.

  5. In the Form Controls toolbar, choose View Classes, and then choose ActiveX Controls.

To display an OLE object from a General field

  1. In the Form Designer, add an OLE Bound control to your form.

  2. Specify the General field that contains the data by setting the object's ControlSource property.

    For example, if the table name is Inventory and the General field name is Current, then set the ControlSource property to Inventory.Current.

You can also display an OLE object from a General field programmatically:

Code

Comments

frm1 = CREATEOBJECT("form")

Create form.

frm1.ADDOBJECT("olb1",

"oleboundcontrol")

Add control.

frm1.olb1.ControlSource =

"Inventory.Current"

Bind the data to the control.

frm1.olb1.Visible = .T.

frm1.Visible = .T.

Make the control and form visible.

See Also

Tasks

How to: Edit OLE Objects in Forms and Tables

Other Resources

Working with OLE Objects