Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel Automation

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
0
0
I have two questions about Excel automation and foxpro.

1. I have opened an excel spreadsheet, made my programmatic changes, I issue the Save() and excel pops up a messagebox and tells me that the file already exists (becuase the spreadsheet was generated use the COPY command). How do I eliminate the messagebox? Do I even need to save spreadsheet again?

2. How do I close the spreadsheet without making it visible (visible = .T.) and closing with excel? I have read and read and I can not find out how.

Thanks

BTW, I know there is more to the Save() than just that, I am just assuming that anyone who can answer my question understands what I am referring to. :)
 
If it has never been made visible, then just releasing the variable holding the excel.application variable will release it. It is stubborn if you make it visible (even if you then make it invisible.) Except... for an undocumented method of the application object: "Quit"!!

Have unsaved changes will probably still pop up that dialog first:
Just check the workbook's "Saved" property, if it is .T., then you Don't have to save again.

Just check if the file you want to save it as already exists, and delete it if it does.

x=create('excel.application')
x.Visible = .T.
x.workbooks.add
x.workbooks(1).worksheets(1).cells(1,1)='Test'
x.workbooks.add
x.workbooks(2).worksheets(1).cells(1,1)='Test 2'
WAIT WINDOW "Press a key to continue"
NewName = Fullpath('TestBook.xls')
IF FILE(NewName)
DELETE FILE (NewName)
ENDIF
x.Workbooks(1).SaveAs(NewName)
x.WorkBooks(1).Close
* Now what used to be Workbooks(2) is (1)
NewName2 = FullPath('TestBook.xls')
IF FILE(NewName2)
DELETE FILE (NewName2)
ENDIF
x.Workbooks(1).SaveAs(NewName2)
x.workbooks(1).worksheets(1).cells(2,1)='Test'
x.WorkBooks(1).Saved = .T.
x.WorkBooks(1).Close
WAIT WINDOW "Press a key to continue and close excel"
x.Quit
RELEASE x
 
oExcel=CreateObject("Excel.Application")
*---show User the file
oExcel.visible=.T.
*----turn off the warning message about file exists
oExcel.DisplayAlerts=.f.
*----------------------------------------------------
*---now open the template
oExcel.Workbooks.Open(mtmplfile)
*---now save the template
oExcel.ActiveWorkbook.SaveAs(mxlsnuFile)

oExcel.ActiveWorkbook.Save
oExcel.quit
release oExcel
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top