In a previous post, I showed you how to remove comments from a Word file. In today's post, I am going to show you how to accomplish the same scenario, but this time with Excel and PowerPoint files. Excel and PowerPoint have a Document Inspector feature, which is able to remove multiple types of data/content, including comments. This feature works great for client side solutions, but how do you cleanse these files of comments on the server? The Open XML SDK can accomplish these scenarios in just a few lines of code.

Note: The code showed in this post is backwards compatible with version 1 of the SDK.

Solution for PowerPoint

Imagine I start off with a PowerPoint deck that has multiple slides and comments. Here is a screenshot of a comment within a PowerPoint slide:

To remove comments from a PowerPoint deck we need to take the following actions:

  1. Open up the PowerPoint deck via the Open XML SDK
  2. Access the main presentation part, which will give us access to all the slide parts within the package
  3. Delete the comment part associated with each slide part
  4. Save changes made to the deck

If you want to jump straight into the code, feel free to download this solution here.

The Code

The code is pretty easy and maps 1:1 to the steps I mentioned above:

static void RemovePowerPointComments(string filename)
{
using (PresentationDocument myPresentation = PresentationDocument.Open(filename, true))
{
PresentationPart presentationPart = myPresentation.PresentationPart;
foreach (SlidePart slide in presentationPart.SlideParts)
{
slide.DeletePart(slide.SlideCommentsPart);
}
}
}

End Result

Running this method, I end up with a presentation void of comments. Pretty easy!

Here is a screenshot of the final presentation:

Solution for Excel

This solution is very similar to the PowerPoint solution.

Imagine I start off with an Excel workbook that has multiple worksheets, where each worksheet contains multiple comments. Here is a screenshot of a comment within a worksheet cell:

To remove comments from an Excel workbook we need to take the following actions:

  1. Open up the Excel workbook via the Open XML SDK
  2. Access the main workbook part, which will give us access to all the worksheet parts within the package
  3. Delete all comment parts associated with each worksheet part
  4. Save changes made to the workbook

If you want to jump straight into the code, feel free to download this solution here.

The Code

This code is very similar to the code used to remove comments from a PowerPoint deck:

static void RemoveExcelComments(string filename)
{
using (SpreadsheetDocument myWorkbook = SpreadsheetDocument.Open(filename, true))
{
WorkbookPart workbookPart = myWorkbook.WorkbookPart;
foreach (WorksheetPart sheet in workbookPart.WorksheetParts)
{
sheet.DeleteParts<WorksheetCommentsPart>(sheet.GetPartsOfType<WorksheetCommentsPart>());
}
}
}

End Result

Running this method, I end up with a workbook void of comments.

Here is a screenshot of the final workbook:

Zeyad Rajabi