Search code examples
visual-foxproexcel-automation

excel close a specific worksheet


I writing a foxpro program that generate report from the excel. In the final process, there are two(2) excel generated by the program. I created a new workbook and copy the two output excel sheet into new workbook. How can I close the two excel file?

my code

**creating new workbook
XL = tmp.application
XL.workbooks.ADD()
XL.visible = .T.
XLsheet = XL.ActiveSheet

**opening the aging excel output
sheetdir=SYS(5)+SYS(2003)+"\temp\EXCEL1.xls"
XL.workbooks.OPEN(sheetdir)
XL.worksheets.COPY(XLsheet)
XL.ActiveSheet.UsedRange.EntireColumn.Autofit
XL.ActiveSheet.name="P1"
xl.cells(1,1).value="xx"

XLsheet = XL.ActiveSheet
**opening the unvalidated excel output
sheetdir=SYS(5)+SYS(2003)+"\temp\EXCEL2.xls"
XL.workbooks.OPEN(sheetdir)
XL.worksheets.COPY(XLsheet)
XL.ActiveSheet.UsedRange.EntireColumn.Autofit
XL.ActiveSheet.name="P2"

Solution

  • Here's what I do:

    #define xlNormal 39  && defines the version of Excel
    oxl = createobject("Excel.Application")
    oxl.Workbooks.add()
    oBook = oxl.ActiveWorkbook()
    oxl.visible = .f.
    osheet = oxl.activesheet
    
     && update the worksheet
    
     oxl.DisplayAlerts = .f. && do not invoke any dialog 
    
     oBook.saveas(mfilex,xlNormal )
     && you can use oBook.Close() if you just want to close the book without saving
    
     oxl.quit()
     release oxl