tags:

views:

915

answers:

2

I'm building an MS Word add-in that has to gather all comment balloons from a document and summarize them in a list. My result will be a list of ReviewItem classes containing the Comment itself, the paragraph number and the page number on which the commented text resides.

Part of my code looks like this:

    private static List<ReviewItem> FindComments()
    {
        List<ReviewItem> result = new List<ReviewItem>();
        foreach (Comment c in WorkingDoc.Comments)
        {
            ReviewItem item = new ReviewItem()
            {
                Remark = c.Reference.Text,
                Paragraph = c.Scope. ???, // How to determine the paragraph number?
                Page = c.Scope. ??? // How to determine the page number?
            };
            result.Add(item);
        }
        return result;
   }

The Scope property of the Comment class points to the actual text in the document the comment is about and is of type Microsoft.Office.Interop.Word.Range. I can't work out how to determine what page and which paragraph that range is located.

With paragraph number, I actually mean the "numbered list" number of the paragraph, such as "2.3" or "1.3.2".

Any suggestions? Thanks!

A: 

Try this for page number:

Page = c.Scope.Information(wdActiveEndPageNumber);

Which should give you a page number for the end value of the range. If you want the page value for the beginning, try this first:

Word.Range rng = c.Scope.Collapse(wdCollapseStart);
Page = rng.Information(wdActiveEndPageNumber);

For paragraph number, see what you can get from this:

c.Scope.Paragraphs; //Returns a paragraphs collection

My guess is to take the first paragraph object in the collection the above returns, get a new range from the end of that paragraph to the beginning of the document and grab the integer value of this:

[range].Paragraphs.Count; //Returns int

This should give the accurate paragraph number of the beginning of the comment range.

Mike Regan
Thanks for your answer, it gave me a jump start. I'd vote it up but I don't have 15 reputation yet :-)One remark though: your code samples required some rewriting in my C# VSTO AddIn project.object direction = WdCollapseDirection.wdCollapseStart;range.Collapse(ref direction);int pageNumber = (int) range.get_Information(WdInformation.wdActiveEndPageNumber);The paragraph number didn't really work out for me, but I see now that my question needs a bit of rephrasing on that topic. Thanks anyhow!
Roy
A: 

With the help Mike Regan gave me in his answer (thanks again Mike), I managed to work out a solution that I want to share here. Maybe this also clarifies what my goal was. In terms of performance, this might not be the fastest or most efficient solution. Feel free to suggest improvements.

The result of my code is a list of ReviewItem classes, that will be processed elsewhere. Without further ado, here's the code:

/// <summary>
/// Worker class that collects comments from a Word document and exports them as ReviewItems
/// </summary>
internal class ReviewItemCollector
{
    /// <summary>
    /// Working document
    /// </summary>
    private Word.Document WorkingDoc = new Word.DocumentClass();

    /// <summary>
    /// Extracts the review results from a Word document
    /// </summary>
    /// <param name="fileName">Fully qualified path of the file to be evaluated</param>
    /// <returns></returns>
    public ReviewResult GetReviewResults(string fileName)
    {
        Word.Application wordApp = null;
        List<ReviewItem> reviewItems = new List<ReviewItem>();

        object missing = System.Reflection.Missing.Value;

        try
        {
            // Fire up Word
            wordApp = new Word.ApplicationClass();

            // Some object variables because the Word API requires this
            object fileNameForWord = fileName;
            object readOnly = true;

            WorkingDoc = wordApp.Documents.Open(ref fileNameForWord,
                ref missing, ref readOnly,
                ref missing, ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing);

            // Gather all paragraphs that are chapter headers, sorted by their start position
            var headers = (from Word.Paragraph p in WorkingDoc.Paragraphs
                           where IsHeading(p)
                           select new Heading()
                           {
                               Text = GetHeading(p),
                               Start = p.Range.Start
                           }).ToList().OrderBy(h => h.Start);

            reviewItems.AddRange(FindComments(headers));

            // I will be doing similar things with Revisions in the document
        }
        catch (Exception x)
        {
            MessageBox.Show(x.ToString(), 
                "Error while collecting review items", 
                MessageBoxButtons.OK, 
                MessageBoxIcon.Error);
        }
        finally
        {
            if (wordApp != null)
            {
                object doNotSave = Word.WdSaveOptions.wdDoNotSaveChanges;
                wordApp.Quit(ref doNotSave, ref missing, ref missing);
            }
        }
        ReviewResult result = new ReviewResult();
        result.Items = reviewItems.OrderBy(i => i.Position);
        return result;
    }

    /// <summary>
    /// Finds all comments in the document and converts them to review items
    /// </summary>
    /// <returns>List of ReviewItems generated from comments</returns>
    private List<ReviewItem> FindComments(IOrderedEnumerable<Heading> headers)
    {
        List<ReviewItem> result = new List<ReviewItem>();

        // Generate ReviewItems from the comments in the documents
        var reviewItems = from Word.Comment c in WorkingDoc.Comments
                          select new ReviewItem()
                          {
                              Position = c.Scope.Start,
                              Page = GetPageNumberOfRange(c.Scope),
                              Paragraph = GetHeaderForRange(headers, c.Scope),
                              Description = c.Range.Text,
                              ItemType = DetermineCommentType(c)
                          };

        return reviewItems.ToList();
    }

    /// <summary>
    /// Brute force translation of comment type based on the contents...
    /// </summary>
    /// <param name="c"></param>
    /// <returns></returns>
    private static string DetermineCommentType(Word.Comment c)
    {
        // This code is very specific to my solution, might be made more flexible/configurable
        // For now, this works :-)

        string text = c.Range.Text.ToLower();

        if (text.EndsWith("?"))
        {
            return "Vraag";
        }
        if (text.Contains("spelling") || text.Contains("spelfout"))
        {
            return "Spelling";
        }
        if (text.Contains("typfout") || text.Contains("typefout"))
        {
            return "Typefout";
        }
        if (text.ToLower().Contains("omissie"))
        {
            return "Omissie";
        }

        return "Opmerking";
    }

    /// <summary>
    /// Determine the last header before the given range's start position. That would be the chapter the range is part of.
    /// </summary>
    /// <param name="headings">List of headings as identified in the document.</param>
    /// <param name="range">The current range</param>
    /// <returns></returns>
    private static string GetHeaderForRange(IEnumerable<Heading> headings, Word.Range range)
    {
        var found = (from h in headings
                     where h.Start <= range.Start
                     select h).LastOrDefault();

        if (found != null)
        {
            return found.Text;
        }
        return "Unknown";
    }

    /// <summary>
    /// Identifies whether a paragraph is a heading, based on its styling.
    /// Note: the documents we're reviewing are always in a certain format, we can assume that headers
    /// have a style named "Heading..." or "Kop..."
    /// </summary>
    /// <param name="paragraph">The paragraph to be evaluated.</param>
    /// <returns></returns>
    private static bool IsHeading(Word.Paragraph paragraph)
    {
        Word.Style style = paragraph.get_Style() as Word.Style;
        return (style != null && style.NameLocal.StartsWith("Heading") || style.NameLocal.StartsWith("Kop"));
    }

    /// <summary>
    /// Translates a paragraph into the form we want to see: preferably the chapter/paragraph number, otherwise the
    /// title itself will do.
    /// </summary>
    /// <param name="paragraph">The paragraph to be translated</param>
    /// <returns></returns>
    private static string GetHeading(Word.Paragraph paragraph)
    {
        string heading = "";

        // Try to get the list number, otherwise just take the entire heading text
        heading = paragraph.Range.ListFormat.ListString;
        if (string.IsNullOrEmpty(heading))
        {
            heading = paragraph.Range.Text;
            heading = Regex.Replace(heading, "\\s+$", "");
        }
        return heading;
    }

    /// <summary>
    /// Determines the pagenumber of a range.
    /// </summary>
    /// <param name="range">The range to be located.</param>
    /// <returns></returns>
    private static int GetPageNumberOfRange(Word.Range range)
    {
        return (int)range.get_Information(Word.WdInformation.wdActiveEndPageNumber);
    }
}
Roy