How To Use Save As Command For Excel File

kasundha

Member
hi all,
im using excel sheet template in my program and i want to save it in different location with different name.
this is my sample code.

DEFINE VARIABLE chExcel AS COM-HANDLE NO-UNDO.
DEFINE VARIABLE chWorksheet AS COM-HANDLE NO-UNDO.
DEFINE VARIABLE chWorkbook AS COM-HANDLE NO-UNDO.
DEFINE VARIABLE xSaveAsName AS CHARACTER NO-UNDO INIT "E:\kasun\test.xlsx".

CREATE "excel.application" chExcel.

chExcel:Workbooks:Open("D:\Opensoft\GL\DOC\RECOVERY DASHBOARD TEMPLATE.xlsx").

chExcel:visible = FALSE.

os-command silent del value(xSaveAsName).
chExcel:DisplayAlerts = FALSE.

chExcel:ActiveSheet:SaveAs(xSaveAsName,6,,,,,,,TRUE).


chWorkBook:close.
chExcel:close.


RELEASE OBJECT chWorksheet NO-ERROR.
RELEASE OBJECT chWorkbook NO-ERROR.
RELEASE OBJECT chExcel NO-ERROR.

=======================================

and it gives following errors.

---------------------------
Error (Press HELP to view stack trace)
---------------------------
Error occurred while accessing component property/method: SaveAs.
Cannot access 'test.xlsx'.
Error code: 0x80020009 C:\PROGRESS\WRK\p02188_Untitled1.cmp (5890)
---------------------------
OK Help
---------------------------
---------------------------
Error (Press HELP to view stack trace)
---------------------------
Invalid component-handle referenced while processing method/statement: close.

C:\PROGRESS\WRK\p02188_Untitled1.cmp (5884)
---------------------------
OK Help
---------------------------
 

oli

Member
The SaveAs method refers to a Workbook object, not a Worksheet.
Beside this, you specify the xlFileFormat 6 (i.e. CSV), while the filename ends with ".xlsx".

Try this:
Code:
chWorkbook:SaveAs(xSaveAsName,-4143 /*DefaultFormat*/,/*Password*/,/*WriteResPassword*/,/*ReadOnlyRecommended*/,/*CreateBackup*/,/*AccessMode*/,/*ConflictResolution*/,/*AddToMru*/,/*TextCodepage*/,/*TextVisualLayout*/,TRUE /*Local*/).


References
Workbook.SaveAs Method (Excel)
XlFileFormat Enumeration (Excel)
 
Top