Skip to main content

Notifications

Microsoft Dynamics AX (Archived)

How to delete excel sheet with content?

Posted on by 559

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

  • André Arnaud de Calavon Profile Picture
    André Arnaud de Cal... 291,232 Super User 2024 Season 2 on at
    RE: How to delete excel sheet with content?

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

  • Rudi Hansen Profile Picture
    Rudi Hansen 3,987 on at
    RE: How to delete excel sheet with content?

    Please mark the Answer as being the answer then.

  • Suggested answer
    bernardqin Profile Picture
    bernardqin 559 on at
    RE: How to delete excel sheet with content?

    Thanks your reply. It does work!

  • bernardqin Profile Picture
    bernardqin 559 on at
    RE: How to delete excel sheet with content?

    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.

  • Sohaib Cheema Profile Picture
    Sohaib Cheema 46,610 User Group Leader on at
    RE: How to delete excel sheet with content?

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

  • Suggested answer
    André Arnaud de Calavon Profile Picture
    André Arnaud de Cal... 291,232 Super User 2024 Season 2 on at
    RE: How to delete excel sheet with content?

    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
    Sohaib Cheema 46,610 User Group Leader on at
    RE: How to delete excel sheet with content?

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


     

  • Verified answer
    Rudi Hansen Profile Picture
    Rudi Hansen 3,987 on at
    RE: How to delete excel sheet with content?

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

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,232 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,064 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans