Chapter 10: Sharing Information with Other Applications

With Visual FoxPro, you can employ the strengths of other applications by sharing information with them. You can copy and paste data between applications, create mail merges using data from Visual FoxPro tables, link or embed data from other applications, and import or export data between applications. By sharing information, you can save time and avoid errors. For example, instead of entering results from Microsoft® Excel in a table, you can copy the results from Microsoft Excel or link to Microsoft Excel to get the results automatically.

For details on importing or exporting, see Chapter 9, Importing and Exporting Data. For information about Automation, see Chapter 16, Adding OLE, in  the Programmer’s Guide.

This chapter discusses:

Sharing Information Quickly

You can quickly share data between Visual FoxPro and other applications or data sources by copying and pasting data. You can also use the Mail Merge Wizard to expedite mass mailings.

Copying and Pasting Data

If you want to share small amounts of data quickly and easily, but don’t want to share entire files, select the data manually, cut or copy it, and then paste it wherever you want it. For example, copy selected data from a form and then paste it into a Microsoft Word document, or copy selected cells from a Microsoft Excel worksheet and then paste them into a General field in a table. You can even copy and paste data into mail messages and send the messages from within Visual FoxPro.

Tip   To share entire files, link or embed them. For details about linking and embedding, see Sharing Information Dynamically, later in this chapter.

To paste data from another application into a General field

  1. Copy the data to the Clipboard.

  2. Open and browse the table containing the General field in which you want to paste the data.

  3. Double-click the General field.

  4. Embed the data by choosing Paste or Paste Special from the Edit menu.

    -or-

    Link to the data by choosing Paste Special from the Edit menu and then selecting Paste Link in the Paste Special dialog box.

    Paste Special dialog box

Tip   You can save disk space when linking or embedding by selecting the Display As Icon check box. When this is selected, Visual FoxPro displays the icon instead of the data.

Creating Mail Merges

A common way of sharing data is to mail letters created from addresses stored in Visual FoxPro and templates stored in Microsoft Word. With Visual FoxPro, you can create a Word document containing data from a Visual FoxPro table by using the Mail Merge Wizard.

To use the Mail Merge Wizard

  1. From the Tools menu, choose Wizards.

  2. From the submenu, choose Mail Merge.

  3. Follow the directions in the wizard.

Sending Mail from Visual FoxPro

If you have a mail program installed, you can send mail from Visual FoxPro.

To send mail

You can also send mail from an application by using the MAPI Session Control and MAPI Messages Control For more information on these controls, see the ActiveX Controls Overview.

Sharing Information Dynamically

In Visual FoxPro, you can dynamically include data from other applications and still have those applications maintain the data by linking or embedding it in your application. For example, you can link or embed a Word document in a General field or you can link or embed a Microsoft Excel worksheet on a form.

When you dynamically link data, it is stored in its source file, not in your Visual FoxPro table or form. The table or form stores only the location of the source and displays a representation of the linked data. Linked data is updated when the source file changes and retains a connection to its source file, unless you break the connection (see Changing or Breaking Links to Data later in the chapter).

Note   Files with an .avi extension are always linked.

When you dynamically embed data, it is stored only in your table or form. This data does not retain a connection to its source file. If changes are made to the source file, they do not appear in your Visual FoxPro application.

Embedding and linking data

Create links when:

Embed data or graphics when:

Use the following guidelines to decide on linking or embedding:

To Use this method Details
Create applications you can use on other computers or edit, even if the source files are not present. Embed the data. To edit the data, all applications from which the data come must be installed on the computer.
Include data that might not always be available, such as data stored on a server. Embed the data. All the necessary information is included with the embedded data.
Include a very large file, such as a video or sound clip. Create a link to the source file. Your application stores only the link, keeping the size of the application manageable.
Include data that is maintained separately in its source application. Create a link to the source application. Your application reflects any changes made to the source application.

The following sections describe how to add data to tables, add data to forms, change or break links to data, display linked or embedded data from tables, and edit linked or embedded data.

Inserting Data in Tables

You can include data from other applications in a Visual FoxPro table by inserting the data into General fields. For example, if you have pictures of products, you can include them in a table containing other product information by inserting them into a General field.

To insert data into a General field

  1. Open and browse the appropriate table.

  2. Double-click the General field in which you want to insert the data.

  3. From the Edit menu, choose Insert Object.

    The Insert Object dialog box appears, listing the types of data you can insert into the field.

    Insert Object dialog box

  4. To create data of your own, select Create New and then choose the appropriate type of data.

    -or-

    To use existing data, select Create from File. Then, type the name of the file containing the data, or choose Browse and select the appropriate file. If you want to link to the file, select Link.

    Insert Object dialog box

Changing or Breaking Links to Data in Tables

After linking to some data, you might want to use a different data source, or you might want to embed rather than link. To change the data source, you must change the link; and to embed, you must break the link.

To change the link to data in a General field

  1. Open and browse the appropriate table.

  2. Double-click the General field containing the data whose link you want to change.

  3. From the Edit menu, choose Links. The Links dialog box appears.

    Links dialog box

    Note   Choose Automatic to automatically update data any time it changes in the source application, or Manual if you want to manually update data.

  4. In the Links dialog box, choose Change Source.

    The Change Source dialog box appears.

    Change Source dialog box

  5. Specify the new data source and choose OK.

  6. In the Links dialog box, choose Close to complete the change.

To break a link to data in a General field

  1. Open and browse the appropriate table.

  2. Double-click the General field containing the data whose link you want to break.

  3. From the Edit menu, choose Links.

  4. In the Links dialog box, choose Break Link.

  5. Choose Close.

When the link is broken, the data becomes static.

Displaying Embedded or Linked Data on Forms

You can enhance the design of forms by linking or embedding data such as Word documents or Microsoft Excel worksheets. Similarly, you can enhance reports by displaying linked or embedded data from General fields. For details, see “Adding a General Field” in Chapter 7, Designing Reports and Labels.

To link or embed data on a form

  1. Create or open a form.

  2. On the Form Controls toolbar, select the OLE Container Control button and drag to size on the form.

    The Insert Object dialog box appears, listing the types of files you can link or embed.

  3. Create a new file by selecting Create New.

    -or-

    Use an existing file by selecting Create from File. Type the name of the file containing the data or choose Browse and select the appropriate file. Additionally, if you want to link to the file, select Link.

Changing or Breaking Links to Data on Forms

You change or break a link to data displayed on a form by using the Links command on the Edit menu.

To change a link to data on a form

  1. Open the appropriate form.

  2. Select the object whose link you want to change.

  3. From the Edit menu, choose Links.

  4. In the Links dialog box, choose Change Source.

  5. In the Change Source dialog box, specify the new data source and choose OK.

  6. In the Links dialog box, choose Close to complete the change.

To break a link to data on a form

  1. Open the appropriate form.

  2. Select the object whose link you want to break.

  3. From the Edit menu, choose Links.

  4. In the Links dialog box, choose Break Link.

  5. Choose Close.

When the link is broken, the data becomes static.

Displaying Embedded or Linked Data from Tables

On a form or report, you can display linked or embedded data from General fields in a table. For example, suppose you have a product table in which each record includes a General field containing a brochure (a Word document) about a product. On a form, you could display selected information from the records — including the brochure describing the product.

To display data from a General field on a form

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

    On the form, Visual FoxPro creates an object through which you can view the data at run time.

  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.

  3. On the form, add buttons or menu commands for browsing the General field specified in the ControlSource property.

To display data from a General field on a report

Including Data in a Report

When creating a report, you can link or embed pictures or icons directly into the report from an outside source using the image container. Other forms of data, such as Word documents or Microsoft Excel spreadsheets, can be included only if they were previously linked or embedded into a General field in a Visual FoxPro table. For more information, see Chapter 7, Designing Reports and Labels.

Editing OLE Objects in Forms and Tables

You can edit linked or embedded data in a table or form while designing the table or form. In addition, you can edit the data in a form while running the form. The following procedures describe the ways you can edit data.

To edit data in a General field in a table

  1. Open and browse the appropriate table.

  2. Go to the record containing the General field you want to edit.

  3. Double-click the General field to display the editing window.

  4. In the editing window, double-click to edit the data.

To edit embedded or linked objects on a form while designing the form

  1. Select the object representing the data.

  2. With the mouse pointer over the object, click the right mouse button.

  3. From the shortcut menu, choose the object type, and then choose Edit or Open.

To edit objects or data from General fields on a form while running the form

  1. From the Edit menu, select the object.

    For example, if the object is a Microsoft Excel worksheet, select Spreadsheet Object from the Edit menu.

    Tip   You can also right-click on the object if its AutoVerbMenu property is set to True. Choose the object type, and then choose Edit or Open.

  2. To edit the object visually, choose Edit from the submenu.

    -or-

    To edit the object in a separate window, choose Open from the submenu.

    Caution   If you change the data in an object while running a form, Visual FoxPro saves the changes only if the data is linked or is associated with an OLE Bound control. Changes associated with an OLE Container control are not saved.

    Editing visually

If you edit in the Visual FoxPro window, menus from the application you’re using replace or merge with Visual FoxPro menus. For example, if you’re using Microsoft Excel, the Excel Edit menu replaces the Visual FoxPro Edit menu. However, the Visual FoxPro File, Program, and Window menus are not replaced.

Replacing Linked or Embedded Data with Static Data

You can replace linked or embedded data with stand-alone bitmaps by using the application that created the data.

To replace linked or embedded data with a bitmap

  1. Start the application that created the data by choosing the object containing the data.

  2. In the application, copy the object’s presentation to the Clipboard.

    For example, if the application has a Copy Picture command, use it to copy the object’s presentation.

  3. From the Edit menu in Visual FoxPro, choose Paste Special.

  4. In the Paste Special dialog box, choose Paste Option.

  5. Choose the Picture (Metafile) option.