/// <summary> /// Find the previous story range (similar to Word's .NextStoryRange) /// </summary> /// <param name="SelectionRange">The starting range.</param> /// <returns>A Range containing the previous story range.</returns> internal static Word.Range PreviousStoryRange(Word.Range SelectionRange) { Word.Range PreviousRange = null; Word.Range CurrentRange = null; switch (SelectionRange.StoryType) { case Word.WdStoryType.wdEvenPagesHeaderStory: case Word.WdStoryType.wdPrimaryHeaderStory: case Word.WdStoryType.wdEvenPagesFooterStory: case Word.WdStoryType.wdPrimaryFooterStory: case Word.WdStoryType.wdFirstPageHeaderStory: case Word.WdStoryType.wdFirstPageFooterStory: case Word.WdStoryType.wdTextFrameStory: CurrentRange = SelectionRange.Document.StoryRanges[SelectionRange.StoryType]; while (!SelectionRange.InStory(CurrentRange) && CurrentRange.NextStoryRange != null) { PreviousRange = CurrentRange; CurrentRange = CurrentRange.NextStoryRange; } return(PreviousRange); default: return(null); } }
/// <summary> /// Finds the index of the section with the specified header/footer. /// </summary> /// <param name="Document">The document to search.</param> /// <param name="SelectionRange">The range containing the specified header/footer.</param> /// <returns>The ordinal position of the section containing the header/footer.</returns> internal static int FindSectionWithHeaderFooter(Word.Document Document, Word.Range SelectionRange) { for (int i = 1; i <= Document.Sections.Count; i++) { if (IsHeader(SelectionRange.StoryType)) { if (SelectionRange.InStory(Document.Sections[i].Headers[GetHeaderFooterType(SelectionRange.StoryType)].Range)) { return(i); } } else { if (SelectionRange.InStory(Document.Sections[i].Footers[GetHeaderFooterType(SelectionRange.StoryType)].Range)) { return(i); } } } throw new ArgumentException("could not locate story for range"); }