Microsoft KB Archive/252615

= OLE examples for taking control of Excel from Visual FoxPro =

Article ID: 252615

Article Last Modified on 1/24/2005

-

APPLIES TO


 * Microsoft Visual FoxPro 3.0 Standard Edition
 * Microsoft Visual FoxPro 3.0b Standard Edition
 * Microsoft Visual FoxPro 5.0 Standard Edition
 * Microsoft Visual FoxPro 5.0a
 * Microsoft Visual FoxPro 6.0 Professional Edition
 * Microsoft Visual FoxPro 7.0 Professional Edition
 * Microsoft Visual FoxPro 8.0 Professional Edition
 * Microsoft Visual FoxPro 9.0 Professional Edition

-



This article was previously published under Q252615



SUMMARY
Dbf2xls.exe is a sample that provides Microsoft Visual FoxPro code to control Microsoft Excel by using OLE Automation.



MORE INFORMATION
The following file is available for download from the Microsoft Download Center:

Download the Dbf2xls.exe package now. Release Date: Mar-20-2000

For additional information about how to download Microsoft Support files, click the following article number to view the article in the Microsoft Knowledge Base:

119591 How to obtain Microsoft support files from online services

Microsoft scanned this file for viruses. Microsoft used the most current virus-detection software that was available on the date that the file was posted. The file is stored on security-enhanced servers that help to prevent any unauthorized changes to the file.

The dbf2xls.exe file contains the following files:

VFPExcel.prg demonstrates, by example, the means by which to create a fully formatted and ready to print Microsoft Excel file using OLE Automation from within Microsoft Visual FoxPro. This program requires the installation of Microsoft Excel version 7.0 (Excel for Office 95) or higher. The program is capable of functioning in Microsoft Visual FoxPro version 3.0 and higher. If this program is run on a computer with no installed printer, functionality associated with Microsoft Excel Page Setup features produces errors. These can be ignored.

The operation performed within this program first uses a COPY TO TYPE XL5 command. While OLE can be used to pass data from Visual FoxPro to Excel, the COPY TO command is vastly more efficient. Once the Excel file is created, the program creates an OLE instance of Microsoft Excel and opens the temporary file created with the COPY TO command. The program stores the temporary file in the Microsoft Visual FoxPro root directory as determined by the HOME function.

NOTE: Any partial installation of Microsoft Visual FoxPro that does not include the appropriate OLE or COM DLLs causes this program to fail when it attempts to instantiate (through OLE) the Microsoft Excel Application Object.

To use this program
A table, view, or cursor must be open in the currently selected work area. The table, view, or cursor must not contain any memo field or fields. This program creates a Microsoft Excel file and stores it in the directory containing the source table. The name assigned to the created file is the same as the one provided by the ALIAS function. The extension of the derived file is .xls.

The program utilizes a parameter passed as a numeric value to determine the paper size and print orientation. The parameter values are shown here:
 * 1 = letter size paper, portrait orientation (1,1)
 * 2 = letter size paper, landscape orientation (1,2)
 * 3 = legal size paper, portrait orientation (5,1)
 * 4 = legal size paper, landscape orientation (5,2)

If no parameter is passed, the default value will be 1.

Important items to remember

 * This program does not work with a table, view, or cursor that contains a memo field or fields.


 * Cursors and Views that represent subsets of a single table or represent data combined from two or more tables can be temp (.tmp) files that are stored in the computer's assigned TEMP directory.


 * This program places the entire contents of the currently open file into a resulting Microsoft Excel file. If you want to use only a subset of the data, this action needs to be taken prior to running this program. This is especially true, and particularly important, when using larger tables.


 * The program code is thoroughly commented to describe all processes that are occurring.

Step-by-step process for using Vfpexcel.prg
 Download the self-extracting executable file and expand the Vfpexcel.prg, Customer.dbf, and Readme.txt files. Start Microsoft Visual FoxPro. From within Microsoft Visual FoxPro, set the default directory to the folder containing the extracted Vfpexcel.prg, Customer.dbf, and the Readme.txt files. Open the Customer.dbf table. Ensure that the selected work area is the one in which the Customer.dbf file is open.  In the Microsoft Visual FoxPro Command window, type the following line of code: DO vfpexcel.prg WITH 4 The result is a Microsoft Excel file with a name of Customer.xls that is stored in the same location as the customer.dbf table. If you print (or print preview) the file, you see that the paper size is legal and the print orientation is landscape. This was determined by the parameter of 4. 

Additional query words: Dbf2xls

Keywords: kbhowto kbdownload kbinterop kbcodesnippet kbfile kbautomation kbsample KB252615

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.