Partager via


Adding OLE Objects to Applications

You can add OLE objects to tables and forms interactively or programmatically.

Adding OLE Objects to Tables

While you're designing tables for your application, consider whether you need OLE objects in the tables. For instance, suppose you have a product table and want to include Word documents containing formatted descriptions of the products to be sent to potential customers. To include the Word documents, you must define a General field in the table. Then, you add the documents to the table by linking or embedding them in the General field.

To add an OLE object to a table

  1. Use the Table Designer to create a table with General field.

  2. Open the window for the General field by browsing the table and double-clicking the General field or by using the MODIFY GENERAL command.

  3. From the Edit menu, choose Insert Object.

    -or-

    Use the APPEND GENERAL command.

Appending OLE Objects to Tables

You can add OLE objects to tables with the APPEND GENERAL command. With this command, you can import an OLE object from a file and place it in 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.

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.

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.6"

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.6"

In addition, you can replace data in an object by using the DATA keyword of APPEND GENERAL, as the following Microsoft Graph example illustrates.

Refreshing Microsoft Graph

Microsoft Graph is an embeddable application. The values in a Microsoft Graph chart are based on the values in the Microsoft Graph data sheet.

In order to programmatically change the data in a Microsoft Graph chart, you need to construct a string that contains the new data, including tabs, carriage returns, and line feeds, and pass this string to a Microsoft Graph object with the DATA clause of the APPEND GENERAL command.

The following example assumes you have a table, named stock, with, among other fields, date and close for the date and the closing price of the stock. The Microsoft Graph object is stored in the msgraph general field of a table named graph. The example refreshes a graph with stock closing prices from the previous 30 days.

Code Comments
#DEFINE CRLF CHR(13)+CHR(10)
#DEFINE TAB CHR(9)
LOCAL lcData
Define carriage return and tab characters.
SELECT date, close;
  FROM Stock WHERE BETWEEN(date, ;
  DATE(),DATE() - 30) ;
  ORDER BY date INTO CURSOR wtemp
Select the values that you want to update the graph with, in this case, the date and closing values for stocks for the last 30 days.
SELECT wtemp
lcData = " " + ;
  TAB + "Closing Price" + CRLF
SCAN 
  lcData = lcData + DTOC(date)
  lcData = lcData + TAB
  lcData = lcData + ;
  ALLTRIM(STR(close)) + CRLF
ENDSCAN
Build a character string (lcData) of data from the cursor to refresh the graph.

"Closing Price," as the column header, is the text that will be displayed by default in the graph's legend.

SELECT graph
APPEND GENERAL msgraph DATA lcData
Send the new values to the graph in the DATA clause of the APPEND GENERAL command.
USE IN wtemp
Close the cursor.

Note   You can also display OLE objects from General fields in your reports.

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.

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.

Interacting with OLE Objects

If you add an OLE object to a form or General field, you can edit the data and display characteristics of the object at run time or design time.

Note   You cannot edit the data of an OLE object in an OLE Bound control at design time.

Some OLE objects support in-place editing so that you can edit the object in the window used by your application. For example, if you double-click a Microsoft Excel worksheet object in a General field, rather than starting a copy of Microsoft Excel in another window, the menu titles change to reflect the Microsoft Excel menu structure and the default Microsoft Excel toolbars are displayed. You or your application user can then edit the Microsoft Excel object without leaving your application.

Note   You can edit only embedded objects in place, not linked objects.

You can also open the Automation server in another window, edit the data or display characteristics there, and have the new values reflected in your application when you return to it.

To edit an OLE object in place in a General field window

  • From the Edit menu, select the specific object type, and from the submenu, choose Edit.

    For example, if the object is a Word document, select the Document Object menu item; if the object is a Microsoft Graph chart, select the Chart Object menu item.

    -or-

    Double-click the object.

To open the application for an OLE object in a General field window

  • From the Edit menu, select the specific object type, and from the submenu, choose Open.

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

Controlling Menus

When a user is in-place editing an OLE object, the menu bar displays the menus for the OLE object, not the menus for your application. If you create a menu title and want it to be displayed even while the user edits an OLE object, select Negotiate in the Prompt Options dialog box of the Menu Designer. For more information, see Designing Menus and Toolbars, or the NEGOTIATE clause in the DEFINE PAD Command topic.

See Also

Adding a General Field | Design of an OLE Application | Using ActiveX Controls | Adding OLE |Table Designer | MODIFY GENERAL | APPEND GENERAL