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

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

delete a worksheet from excel workbook

(0) ShareShare
ReportReport
Posted on by
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

I have the same question (0)
  • Verified answer
    Vilmos Kintera Profile Picture
    46,149 on at
    RE: delete a worksheet from excel workbook

    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();
    }
  • Community Member Profile Picture
    on at
    RE: delete a worksheet from excel workbook

    I didn't receive any error, but when i check the excel file, i found that the sheet already exist but with empty content.

  • Vilmos Kintera Profile Picture
    46,149 on at
    RE: delete a worksheet from excel workbook

    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?

  • Community Member Profile Picture
    on at
    RE: delete a worksheet from excel workbook

    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!!

  • Vilmos Kintera Profile Picture
    46,149 on at
    RE: delete a worksheet from excel workbook

    The post gave that answer of what you want if you would read more carefully, first reply marked as answer.

  • Community Member Profile Picture
    on at
    RE: delete a worksheet from excel workbook

    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!!

  • Suggested answer
    Vilmos Kintera Profile Picture
    46,149 on at
    RE: delete a worksheet from excel workbook

    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.

Helpful resources

Quick Links

Responsible AI policies

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

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Syed Haris Shah Profile Picture

Syed Haris Shah 9

#2
Martin Dráb Profile Picture

Martin Dráb 2 Most Valuable Professional

#2
Community Member Profile Picture

Community Member 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans