none
In Word, how can I create a Range in the header/footer, similar to using Document.Range(Start, End)? RRS feed

  • Question

  • Is it possible to create a Range in a Word document header or footer?
    Specifically, I am looking for the equivalent of

    Word.Range Document.Range(Start, End)

    Simplified, my code would look as below. I'm getting the current selection, checking what StoryType it is, and then I'd like to create a Range at the start of the selection and use it to insert text (I don't want to overwrite or modify the selection).

    var currentSelection = document.Application.Selection.Range;
    var newRange = document.StoryRanges[currentSelection.StoryType].Range(currentSelection.Start, currentSelection.Start);
    However, document.StoryRanges[StoryType] doesn't have a Range method, only a SetRange, which does not return the range that I need to further manipulate and insert text into.

    Any help is appreciated.

    Monday, May 23, 2016 6:57 AM

Answers

All replies

  • Hi KC_Syd

    Is it possible to create a Range in a Word document header or footer?

    you can use the code like below.

    foreach (Word.Section section in this.Application.ActiveDocument.Sections)
    {
        Word.Range headerRange = section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
        headerRange.Fields.Add(headerRange, Word.WdFieldType.wdFieldPage);
        headerRange.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
    }
    

    you have mentioned that, "I'm getting the current selection, checking what StoryType it is"

    Are you doing a selection in Header?

    Regards

    Deepak


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, May 24, 2016 1:39 AM
    Moderator
  • Hi Deepak,

    Thanks for the suggestion. 

    I am trying to find a consistent, generic way to get a Range for insertion based on the start position of the user's cursor/selection, regardless of where the cursor is (header, footer, main document body etc).

    When the selection is in the main document body, I can user Document.Range(start, end) that returns a Range object that I can work with. I can not seem to find a similar way to create a Range by specifying the start and end and getting that Range object returned to my code so I can work with it when the selection is elsewhere (like header/footer). The reason I was checking for the StoryType was to then somehow try to create the Range in the appropriate location.

    The code you suggested would indeed get me the all section's primary header ranges, but how do I know which one of these the actual selection is in? Also if the header contains some text, for example:

    "Insert here | with some additional text following"

    where the | denotes the current cursor, how do I create a Range at the position of the | to work with? If this was in the document body, I could do a range = document.Range(13,13), but what about in the header?

    I hope that helps clarify. 

    Tuesday, May 24, 2016 3:45 AM
  • Hi KC_Syd,

    I understand the limitation of document.Range(13,13).

    I try to make a search regarding exact function with the help of that you can add your range in header or footer at specific location.

    but unfortunately I did not find anything till now.

    if I find something that can able to do what you want I will provide you.

    Regards

    Deepak


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, May 25, 2016 5:30 AM
    Moderator
  • Hi KC_Syd,

    currently there is no any function that can help you to add range at specific location in header or footer.

    Regards

    Deepak


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, June 6, 2016 9:17 AM
    Moderator
  • You can copy the header range by calling duplicate and then looking for the range with start and end positions.

    Sample:

    private W.Range FindRange(W.Field field)

    {   

    W.Range duplicate = field.Result.Duplicate;   

    duplicate.SetRange(field.Result.Start, field.Result.End);   

    return duplicate;

    }


    Tuesday, July 16, 2019 8:29 AM