I create a job to delete an excel sheet with content but failed, while it works when the sheet is empty. How to delete a sheet which is not empty?
Belowing is the job.
static void Job1(Args _args)
{
SysExcelApplication excel;
SysExcelWorkbooks books;
SysExcelWorkbook book;
SysExcelWorksheets sheets;
SysExcelWorksheet sheet;
excel = SysExcelApplication::construct();
books = excel.workbooks();
books.open("f:\\test.xlsx");
book = books.item(1);
sheets = book.worksheets();
sheet = sheets.itemFromNum(2);
sheet.delete();
excel.visible(true);
}
*This post is locked for comments
Hi,
When you respond to a reply, there is no structure or reference where we can see to which answer you replied on. Can you tell which answer solved your problem? You can click yes or no per answer on the section 'Did this answer your question?'
Please mark the Answer as being the answer then.
Thanks your reply. It does work!
Yes, the sheet number is correct. The sheet2 is deleted if it is empty. After I set cell "A1" to any string, save the excel and run the job again, the sheet cannot be deleted. The job is totally same.
apologies, I thought you are trying to delete file. its excel sheet you are trying. let me remove my verdict
Hi,
You are trying to delete a sheet with internal number 2. Are you sure this internal number matches your sheet number? Probably you can use the command itemFromName(str _name).
Here you go, it does not matter if its excel or word or any other type of file. a file is a file:
boolean fileExists; dialog d; Filename filename; DialogField dialogFilename; ; d = new dialog(); d.caption("select a file to delete."); dialogFilename = d.addField(extendedTypeStr(FilenameOpen)); d.run(); if(d.closedOk()) { filename = dialogFileName.value(); fileExists = WinAPI::fileExists(filename); if(fileExists) { WinAPI::deleteFile(filename); info("File Deleted!"); } }
I think you need to use excel.displayAlerts(false);
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,232 Super User 2024 Season 2
Martin Dráb 230,064 Most Valuable Professional
nmaenpaa 101,156