avr. 07

If you dynamically add worksheets, you may need to delete them also. How to do that ? Simple. You can write a simple function as :

/// <summary>

/// Delete all the worksheet from the given collection, except the ones that are named

/// accordingly to one of the given name

/// </summary>

/// <param name="worksheets">The collection of worksheet we want to clean</param>

/// <param name="excludedNames">The names of the worksheet we want to keep</param>

public static void DeleteWorksheets(IExcel.Sheets worksheets, params string[] excludedNames)

{

   for ( int i = worksheets.Count; i > 0; i-- )

   {

      IExcel.Worksheet sheet = worksheets[i] as IExcel.Worksheet;

      if ( !excludedNames.Contains(sheet.Name) )

         sheet.Delete();

   }

}

How to use it ? Well if you are in a sheet code and that you want to delete all the sheets except the current one, you can do :

ExcelUtils.DeleteWorksheets(Globals.ThisWorkbook.Worksheets, this.Name);

Almost good. You will simply notice that each time Excel wants to delete a sheet, you will get a popup asking to confirm the deletion. How to bypass it ? Let's modify our function as follows :

/// <summary>

/// Delete all the worksheet from the given collection, except the ones that are named

/// accordingly to one of the given name

/// </summary>

/// <param name="worksheets">The collection of worksheet we want to clean</param>

/// <param name="excludedNames">The names of the worksheet we want to keep</param>

public static void DeleteWorksheets(IExcel.Sheets worksheets, params string[] excludedNames)

{

   //1. DisplayAlerts = true : will display confirmation popup for the deletion.

   //   to do a silent deletion, we'll set it to false (but save the old values to restore it)

   bool displayAlerts = worksheets.Application.DisplayAlerts;

   try

   {

      worksheets.Application.DisplayAlerts = false;

 

      for ( int i = worksheets.Count; i > 0; i-- )

      {

         IExcel.Worksheet sheet = worksheets[i] as IExcel.Worksheet;

         if ( !excludedNames.Contains(sheet.Name) )

            sheet.Delete();

      }

   }

   finally

   {

      worksheets.Application.DisplayAlerts = displayAlerts;

   }

}

And here we are !
Tags: |

Commentaires

brinks-alarm-system.com

Posted on lundi, 10 juin 2013 09:59

Can I simply say what a relief to find someone who genuinely understands what they are talking about online. You definitely realize how to bring an issue to light and make it important. A lot more people must check this out and understand this side of the story. It's surprising you aren't more popular given that you surely have the gift.

Look at my page:  brinks-alarm-system.com - http://brinks-alarm-system.com/

home security austin

Posted on mercredi, 12 juin 2013 01:12

There is certainly a great deal to know about this subject. I really like all of the points you made.

Feel free to surf to my homepage:  home security austin - rinpa.altervista.org/.../...nionedavide77.jpg.html

http://homewarrantypolicy.org/

Posted on lundi, 1 juillet 2013 22:38

Nice post. I was checking constantly this weblog and I'm impressed! Extremely helpful info specially the closing phase Smile I handle such info much. I used to be looking for this certain info for a long time. Thanks and good luck.

Here is my site:  http://homewarrantypolicy.org/ - varsityphotos.com/.../Instructions14.JPG.html

home security service

Posted on mardi, 2 juillet 2013 03:02

Heya i'm for the primary time here. I came across this board and I to find It truly useful & it helped me out much. I hope to offer one thing back and aid others such as you helped me.

Feel free to surf to my blog ::  home security service - www.playonlinefree.org/index.php

laserspineoryoga.blogspot.com

Posted on vendredi, 26 juillet 2013 08:07

I’m not that much of a online reader to be honest but your sites really nice, keep it up! I'll go ahead and bookmark your website to come back later on. All the best

My webpage -  laserspineoryoga.blogspot.com - ricksuggs.com/.../medical-staff-credentialing-preparing-in-the-health-of-patients

healthy ways to lose weight

Posted on lundi, 5 août 2013 06:56

Everything is very open with a very clear explanation of the challenges. It was definitely informative. Your site is very helpful. Thanks for sharing!

my website:  healthy ways to lose weight - eastcoastcampinghire.com.au/.../?page=374

www.allthebestvacuumcleaners.com

Posted on mardi, 3 décembre 2013 18:08

It's really a great and useful piece of info. I'm satisfied that you simply shared this helpful info with us. Please keep us up to date like this. Thank you for sharing.

Also visit my web site - dyson vacuum cleaners,  www.allthebestvacuumcleaners.com - www.allthebestvacuumcleaners.com/.../ ,

ps3 emulator

Posted on vendredi, 20 décembre 2013 13:25

However, it is a near-perfect fit for the way our family likes to spend some of their free time. There will always come a time when not even the instructional videos can help you understand what is going on. When you fix the troubleshooter yourself, you'll be able to have it fixed within 1 day or even within 1 hour.

My blog post:  ps3 emulator - http://ps3emulator.emulatorx.net

web page

Posted on samedi, 15 mars 2014 16:45

Hmm is anyone else experiencing problems with the pictures on this blog loading? I'm trying to figure out if its a problem on my end or if it's the blog. Any feedback would be greatly appreciated.

Also visit my blog:  web page - http://ni190508_2.vweb04.nitrado.net/activity/p/86930/

steroidsforsale.biz

Posted on lundi, 9 janvier 2017 14:26

Pingback from steroidsforsale.biz

testosterone cypionate where to buy

Ajouter un commentaire




biuquote
  • Commentaire
  • Aperçu immédiat
Loading