web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

How to delete excel sheet with content?

(0) ShareShare
ReportReport
Posted on by 573

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

I have the same question (0)
  • Verified answer
    Rudi Hansen Profile Picture
    4,079 on at

    I think you need to use excel.displayAlerts(false);

  • Sohaib Cheema Profile Picture
    49,668 Super User 2026 Season 1 on at

    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!"); } }


     

  • Suggested answer
    André Arnaud de Calavon Profile Picture
    306,446 Super User 2026 Season 1 on at

    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).

  • Sohaib Cheema Profile Picture
    49,668 Super User 2026 Season 1 on at

    apologies, I thought you are trying to delete file. its excel sheet you are trying. let me remove my verdict

  • bernardqin Profile Picture
    573 on at

    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.

  • Suggested answer
    bernardqin Profile Picture
    573 on at

    Thanks your reply. It does work!

  • Rudi Hansen Profile Picture
    4,079 on at

    Please mark the Answer as being the answer then.

  • André Arnaud de Calavon Profile Picture
    306,446 Super User 2026 Season 1 on at

    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?'

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.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the June Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans