Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics NAV (Archived)

Delete the contents of Sheet + Excel using .Net Variables

Posted on by 5,567

Hi All

we want to update the contents of a particular sheet in excel or delete the contents of the Excel sheet using .NET Variables(if we use the UpdateBook function in the Excel buffer it will overwrite the data, but data won't deleted) 

Thanks for your help.

Regards

Rajasekhar.Y

*This post is locked for comments

  • mmv Profile Picture
    mmv 11,465 on at
    RE: Delete the contents of Sheet + Excel using .Net Variables

    Good!!!

  • Rajasekhar@MS Profile Picture
    Rajasekhar@MS 5,567 on at
    RE: Delete the contents of Sheet + Excel using .Net Variables

    Hi MMV

    finally using the above we will be able to delete the contents, Thanks

    Regards

    Rajasekhar.Y

  • mmv Profile Picture
    mmv 11,465 on at
    RE: Delete the contents of Sheet + Excel using .Net Variables

    Hi Rajasekhar,

    Did you try the above?

    Best Regards,

    MMV

  • Verified answer
    mmv Profile Picture
    mmv 11,465 on at
    RE: Delete the contents of Sheet + Excel using .Net Variables

    Hi,

    Please find the below code for content deletion:

    XlApp := XlApp.ApplicationClass;

    XlWrkBk := XlHelper.CallOpen(XlApp,'D:\Test.xlsx');

    XlWrkSht := XlWrkBk.Worksheets.Item('Sheet1');

    xlRange := XlWrkSht.Range('A1','A10');

    xlRange.Activate;

    xlRange.Select;

    xlRange.ClearContents;

    XlWrkBk.Save;

    XlWrkBk.Close(TRUE,'D:\Test.xlsx',TRUE);

    XlHelper.CallQuit(XlApp);

    xlRange is a (DotNet) object of Microsoft.Office.Interop.Excel.Range.'Microsoft.Office.Interop.Excel, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'

    Best Regards,

    MMV

  • Rajasekhar@MS Profile Picture
    Rajasekhar@MS 5,567 on at
    RE: Delete the contents of Sheet + Excel using .Net Variables

    Hi

    The clearcontents not working, receiving the below error

    A DotNet variable has not been instantiated. Attempting to call ClearContents in Table Excel Buffer: ClearContents

    Regards

    Rajasekhar.Y

  • mmv Profile Picture
    mmv 11,465 on at
    RE: Delete the contents of Sheet + Excel using .Net Variables

    Hi,

    You may try the below code for clearing:

    Range("F8:J17").Select

       Selection.ClearContents

    Best Regards,

    MMV

  • Rajasekhar@MS Profile Picture
    Rajasekhar@MS 5,567 on at
    RE: Delete the contents of Sheet + Excel using .Net Variables

    Hi MMV

    Server : The sheet will be deleted if it is empty , if there is data the sheet won't deleted

    CLient : Same error

    or is there any way to clear the data in of specific range in the sheet?

    or can we rename the sheet?

    Regards

    Rajasekhar.Y

  • mmv Profile Picture
    mmv 11,465 on at
    RE: Delete the contents of Sheet + Excel using .Net Variables

    Hi Rajasekhar,

    As a trial, please try the below code as it's:-

    All are to be declared as DotNet variables:

    XlApp := XlApp.ApplicationClass;

    XlWrkBk := XlHelper.CallOpen(XlApp,'D:\Testing.xlsx');

    XlWrkSht := XlWrkBk.Worksheets.Item('Sheet2');

    XlWrkSht.Delete;

    XlWrkBk.Save;

    XlWrkBk.Close(TRUE,'D:\Testing.xlsx',TRUE);

    In order to execute the above code, please create an excel file on the above specified path.  Let's try whether it works at your end.

    Please try the same on the server as well as on the client.

    Best Regards,

    MMV

  • Rajasekhar@MS Profile Picture
    Rajasekhar@MS 5,567 on at
    RE: Delete the contents of Sheet + Excel using .Net Variables

    Hi MMV

    Yes, the file exists

    (it is the problem with the .net variable,it  is running on only server, if you try to run the report with the code above we will get the error )

    Regards

    Rajasekhar.Y

  • mmv Profile Picture
    mmv 11,465 on at
    RE: Delete the contents of Sheet + Excel using .Net Variables

    Hi,

    When you paste the above mentioned path on the Windows -> Run command, is the file been getting opened?

    Best Regards,

    MMV

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!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans