HOWTO: Selecting Cells in Excel by Using OLE Automation

Last reviewed: February 18, 1997
Article ID: Q139051
The information in this article applies to:
  • Microsoft Visual FoxPro for Windows, version 3.0, 5.0

SUMMARY

This article shows by example how to use OLE Automation programmatically to create a Microsoft Excel worksheet, and select a range of cells.

MORE INFORMATION

Step-by-Step Example

  1. In Visual FoxPro, create and run a program (.prg file) that contains the following code:

          * Start of program code
          *
          MyF3 = ON("KEY","F3")
          ON KEY LABEL F3 DO getout
    

          oleExcel=CREATEOBJECT("excel.application")  && Starts Microsoft Excel
    
          oleExcel.VISIBLE = .T.               && Makes Microsoft Excel visible
    
          oleTest=CREATEOBJECT("excel.sheet")   && Creates a worksheet
          oleTest.Range("A1:D5").Select         && Selects a range of cells
    
          READ EVENTS
    
          PROCEDURE getout
    
             CLEAR EVENTS
             oleExcel.Quit
             ON KEY LABEL F3 &MyF3
    
          * End of program code
    
    

  2. After Microsoft Excel starts, cells A1 through D5 should be selected.

  3. Switch back to Visual FoxPro, and press the F3 key to terminate Microsoft Excel.


KBCategory: kbole kbcode kbhowto
KBSubcategory: FxprgGeneral VFoxWin
Additional reference words: 3.00 5.00


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 18, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.