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

Notifications

Announcements

No record found.

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,075 on at

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

  • Sohaib Cheema Profile Picture
    49,438 User Group Leader 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
    300,911 Super User 2025 Season 2 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,438 User Group Leader 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,075 on at

    Please mark the Answer as being the answer then.

  • André Arnaud de Calavon Profile Picture
    300,911 Super User 2025 Season 2 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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans