This project has moved and is read-only. For the latest updates, please go here.

C# PowerPoint: Problem deleting comments

Mar 9, 2014 at 2:22 PM
Hi all,

I recently started developing a PowerPoint addin in C# using NetOffice but now ran into problems:

I'm trying to programmatically create and delete comments made by a particular author on a given slide. Creation works fine. However, after I programmatically deleted a comment for the first time, access to comments newly created after that fails with a COMException.

Here are some sample sequences:
  1. delete, create, <access to newly created comment> -> COMException
  2. create, delete, create, <access to newly created comment> -> COMException
  3. create, create, create, delete, create, <access to newly created comment> -> COMException
Access to previously existing comments still works after I used delete.

The following code is a minimal working example of the problem:
using PowerPoint = NetOffice.PowerPointApi;
using NetOffice.OfficeApi.Enums;
using System.Collections.Generic;

namespace CommentMWE
{
    class CommentMWE
    {
        private static string EXAMPLE_PRESENTATION_PATH = "<put your path here>";

        private static string COMMENT_AUTHOR_INITIALS = "BG";
        private static string COMMENT_AUTHOR_NAME = "Bill Gates";


        private static void CreateComment(PowerPoint.Slide slide, string author, string initials, string text)
        {
            PowerPoint.Comments comments = slide.Comments;
            comments.Add(0, 0, author, initials, text);
        }

        private static void DeleteComments(PowerPoint.Slide slide, string author, string initials)
        {
            PowerPoint.Comments comments = slide.Comments;

            //Avoid modifying the iterated collection by collecting
            //the comments to delete in a list and then deleting them after iteration.
            IList<PowerPoint.Comment> commentsToDelete = new List<PowerPoint.Comment>();

            foreach (PowerPoint.Comment comment in comments)
            {
                if (CommentMatches(comment, author, initials))
                {
                    commentsToDelete.Add(comment);
                }
            }

            foreach (PowerPoint.Comment commentToDelete in commentsToDelete)
            {
                commentToDelete.Delete();
                //commentToDelete.Dispose(true);
            }
        }

        private static bool CommentMatches(PowerPoint.Comment comment, string author, string initials)
        {
            if (comment == null)
            {
                return false;
            }

            string commentAuthorInitials = comment.AuthorInitials;

            if (commentAuthorInitials != null && commentAuthorInitials.Equals(initials))
            {
                string commentAuthor = comment.Author;

                if (commentAuthor != null && commentAuthor.Equals(author))
                {
                    return true;
                }
            }

            return false;
        }

        static void Main(string[] args)
        {
            //Create application
            PowerPoint.Application application = new PowerPoint.Application();

            //Load presentation
            PowerPoint.Presentation presentation = application.Presentations.Open(EXAMPLE_PRESENTATION_PATH, MsoTriState.msoFalse);
            PowerPoint.Slide titleSlide = presentation.Slides[1];



            //Exception is thrown when creating a comment after calling DeleteComments(..)

            CreateComment(titleSlide, COMMENT_AUTHOR_NAME, COMMENT_AUTHOR_INITIALS, "Irrelevant 1");
            CreateComment(titleSlide, COMMENT_AUTHOR_NAME, COMMENT_AUTHOR_INITIALS, "Irrelevant 2");
            DeleteComments(titleSlide, COMMENT_AUTHOR_NAME, COMMENT_AUTHOR_INITIALS);

            //Never gets here
            CreateComment(titleSlide, COMMENT_AUTHOR_NAME, COMMENT_AUTHOR_INITIALS, "Irrelevant 3");
            DeleteComments(titleSlide, COMMENT_AUTHOR_NAME, COMMENT_AUTHOR_INITIALS);



            //Save and close presentation
            presentation.Save();
            presentation.Close();

            //Exit PowerPoint
            application.Quit();
            application.Dispose();
        }
    }
}
I searched the forums and the internet in general but came up with no results and spent the better part of two days debugging. Now, I'm at a complete loss and would appreciate any help that you could give me...

Thanks in advance!

P.S.: I have some years of programming experience but am new to NetOffice and C# development in general. So if there is an obvious problem with my code, please do point that out! :)