Search code examples
c#vstooffice-interop

microsoft office interop word: how to know if a paragraph is a caption


I'm writing a VSTO add-ins in C# that parses an Office Word document.

I have to check if each figure of the document has a caption label. I managed to know when a paragraph contains a Figure:

var activeDoc = Globals.ThisAddIn.Application.ActiveDocument;


for (int i = 0; i < activeDoc.Paragraphs.Count; i++)
{
    Paragraph par = activeDoc.Paragraphs[i + 1];

    if (par.Range.InlineShapes.Count == 1)
    {
        // the paragraph has an image
    }
}

but I don't see any ways to know if the paragraph is a caption or simple text.

I tried to use CaptionLabels but it returns the types of the captions [Figure, Table, Equation] and not all the captions in my documents.


Solution

  • I ran a quick test and the paragraph that is a caption has a builtin style applied that's called 'Caption' (par.Style.NameLocal) If that name is always 'Caption' (or you feed it as a parameter) then you can distinguish non-caption paragraphs from captions.

    As a tip: write a little test code and put a breakpoint to examine the objects and find what makes them unique. In this case the paragraph/range style is 'Caption'. This is the most efficient way imo.