none
How to find the position of a Bookmark or a Content Control RRS feed

  • Question

  • I need to extract the "position" of several content controls (that are inside of a bookmark) inserted in a document (the foreach part and name extraction is ok).

    What I mean for position is the chapter/section number, in the sample below I would like to obtain that Req234 is in sect. 4.1
    The 4.1 is obviusly a heading2 paragraph.If it is impossible or too complicated, the Page number inside the document would be ok too.

    Eg.

    4. Title

    Lorem ipsum

    4.1 Subtitle

    Lorem ipsum

    [Req234]

    5. Title

    Thanks in advance

    Sandro


    • Edited by SandroR Friday, July 10, 2015 10:51 AM
    Thursday, July 9, 2015 3:11 PM

Answers

  • Hi Sandro

    Word provides a number of built-in bookmarks that return a number of "oddities", one of picks up the entire Heading Level of the current selection. The following VB code line returns the number of the Heading "above" the current selection:

    Document.Bookmarks("\HeadingLevel").Range.Paragraphs(1).Range.ListFormat.ListString


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, July 10, 2015 8:11 PM
    Moderator
  • Ah, and to get the Page number, use the Range.Information(Word.WdInformation.wdActiveEndPageNumber) method.

    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by SandroR Saturday, July 11, 2015 7:25 AM
    Friday, July 10, 2015 8:12 PM
    Moderator

All replies

  • Hi Sandro

    Word provides a number of built-in bookmarks that return a number of "oddities", one of picks up the entire Heading Level of the current selection. The following VB code line returns the number of the Heading "above" the current selection:

    Document.Bookmarks("\HeadingLevel").Range.Paragraphs(1).Range.ListFormat.ListString


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, July 10, 2015 8:11 PM
    Moderator
  • Ah, and to get the Page number, use the Range.Information(Word.WdInformation.wdActiveEndPageNumber) method.

    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by SandroR Saturday, July 11, 2015 7:25 AM
    Friday, July 10, 2015 8:12 PM
    Moderator
  • Cindy, thanks for the hint... I was able to retrieve the page and the heading of the current the contentControl...

    I achieved this, moving the selection after each control in the loop... Is that the right method? Or having a reference of the contentcontrol, there is a more elegant method ?

     

    Document doc = Globals.ThisAddIn.Application.ActiveDocument; foreach (ContentControl contentControl in Globals.ThisAddIn.Application.ActiveDocument.ContentControls) { //right method?? Range rng = contentControl.Range;
    doc.Range(rng.End, rng.End).Select(); string section = doc.Bookmarks.get_Item(@"\HeadingLevel").Range.Paragraphs[1].Range.ListFormat.ListString; }

    Thank you
    Sandro




    • Edited by SandroR Saturday, July 11, 2015 10:45 AM
    Saturday, July 11, 2015 10:04 AM
  • Hi Sandro

    Unfortunately, there's no more elegant method than to Select - these built-in bookmarks date back to the early 90's, where the programming interface was directly connected to the UI, which means Selection.

    Although I would have thought rng.Select(); would have been enough?


    Cindy Meister, VSTO/Word MVP, my blog

    Saturday, July 11, 2015 3:34 PM
    Moderator
  • Yes it is enough... my knowledge about Selection are of 70's :-)

    Thanks a lot again!

    Sandro

    Saturday, July 11, 2015 4:24 PM