Hi everyone,I'am wondering to know how can i delete an existant worksheet from an excel file . workbooks.open(fileNameOpen); workbook=workbooks.item(1); worksheets = workbook.worksheets() worksheets.itemFromName("mon test").delete(); the delete method can delete just the content not the hole worksheet.Thanks.
*This post is locked for comments
When I try this with AX 2012 R1, using Excel 2016 it just works. I have created a new Test.xlsx file, with worksheets called Sheet1 and Sheet2 with data inside. Then the following AX job does what I need perfectly fine, removes Sheet2 by name and saves the document, as suggested by the other post:
static void WIK_testExcelWorksheetDelete(Args _args) { #define.FileName(@'C:\Temp\test.xlsx') SysExcelApplication excel; SysExcelWorkbooks workBooks; SysExcelWorkbook workBook; SysExcelWorksheets workSheets; SysExcelWorksheet workSheet; excel = SysExcelApplication::construct(); excel.visible(true); excel.displayAlerts(false); try { workBooks = excel.workbooks(); workBooks.open(#FileName); workBook = workBooks.item(1); workSheets = workBook.worksheets(); workSheet = workSheets.itemFromName('Sheet2'); workSheet.delete(); workBook.saveAs(#FileName); workBooks.close(); } catch { error('There was a problem'); } excel.quit(); }
I didn't receive any error, but when i check the excel file, i found that the sheet already exist but with empty content.
Ok, now we are one step forward, once you have confirmed that the marked answer is not helping in your case.
Can you please confirm what is the error message you are getting now when trying to delete the sheet?
it is marked as answer , yes!! but this doesn't work for me, so using excel.displayAlerts(false) didn't fixe my problem that's why i have asked the question again and i usually appriciate the collaboration and the support offred by ax community members! so i have never hesitate because we ask to learn!!! so thank you for your comment!!
The post gave that answer of what you want if you would read more carefully, first reply marked as answer.
I have seen this post, and i have done other tests using excel.displayAlerts(false) etc.., and in my case i need to delete worksheet from an existing excel file, not to delete the hole excel file! Thank you!!
If I google "ax remove excel worksheet", the first hit pointing to this community site does give the correct answer.
community.dynamics.com/.../161249
Please try to search next time to avoid duplication of already answered questions.
Under review
Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.
As AI tools become more common, we’re introducing a Responsible AI Use…
We are honored to recognize Andrés Arias as our Community Spotlight honoree for…
These are the community rock stars!
Stay up to date on forum activity by subscribing.
Syed Haris Shah 9
Martin Dráb 2 Most Valuable Professional
Community Member 2