Pregunta

I'm writing an application which reads out content controls from word documents. It works fine, but I'm not able to get the controls in the header and footer of the document.

Here's my code:

public static List<string> GetContentControlsList(word.Document currentWordDocument)
{
    List<string> contentControlsList = new List<string>();

     try
     {
         ContentControls contentControlsCollection = currentWordDocument.ContentControls;

         if (contentControlsCollection != null)
         {
             if (contentControlsCollection.Count > 0)
             {
                 foreach (ContentControl contentControl in contentControlsCollection)
                 {
                     if (!String.IsNullOrEmpty(contentControl.Title) && !contentControlsList.Contains(contentControl.Title))
                     {
                         contentControlsList.Add(contentControl.Title);
                     }
                 }
             }
         }
     }
     catch (Exception ex)
     {
         // TODO do error handling here
     }

     return contentControlsList;
 }

I am able to get the content controls from text body with this code but I also need header and footer.

¿Fue útil?

Solución

This works (code has to be refactored a little bit but I think the principle is clear):

    public static List<string> GetContentControlsList(word.Document currentWordDocument)
    {
        List<string> contentControlsList = new List<string>();

        try
        {
            ContentControls contentControlsCollection = currentWordDocument.ContentControls;

            if (contentControlsCollection != null)
            {
                if (contentControlsCollection.Count > 0)
                {
                    foreach (ContentControl contentControl in contentControlsCollection)
                    {
                        if (!String.IsNullOrEmpty(contentControl.Title) && !contentControlsList.Contains(contentControl.Title))
                        {
                            contentControlsList.Add(contentControl.Title);
                        }
                    }
                }
            }

            //Get storyRanges from document for header and footer properties
            StoryRanges storyRanges = currentWordDocument.StoryRanges;

            foreach (Range storyRange in storyRanges)
            {
                ContentControls storyRangeControls = storyRange.ContentControls;

                if (storyRangeControls != null)
                {
                    if (storyRangeControls.Count > 0)
                    {
                        foreach (ContentControl control in storyRangeControls)
                        {
                            if (!String.IsNullOrEmpty(control.Title) && !contentControlsList.Contains(control.Title))
                            {
                                contentControlsList.Add(control.Title);
                            }
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            //TODO do error handling here
        }

        return contentControlsList;
    }

Thanks to KazJaw for his comment.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top