none
Range.ListFormat.ListString returns ListString of numbered paragraph instead of selection range RRS feed

  • Question

  • Hi

    I am trying to get the result of a LISTNUM field as displayed in a Word document.

    When the LISTNUM field is within a numbered paragraph, the result is the displayed number of the paragraph, not of that field. Such as:

    When the LISTNUM field is within an UNnumbered paragraph, we're fine:

    To reproduce the behaviour, you take a Word document, start a numbered list on page 1, set the page number as a field in any paragraph but the first, only select the page number field, and execute following line of code, "selection" being the Selection of your document or application add-in:

    MessageBox.Show(selection.Range.ListFormat.ListString);

    So... Does someone know how I can get the result of a LISTNUM field displayed by Word when it is in a numbered paragraph?

    Any help is much appreciated!

    Thanks, Nora

    Friday, January 8, 2016 2:33 PM

Answers

  • Hi Nora

    Haven't a clue, and I can't find anything better...

    As a workaround you might try transferring the range (copy, paste as unformatted text) to a hidden "temp" document. That way no worries about undoing or damaging the original document.

    FWIW putting it in a QUOTE field makes it invisible.

    Creating a Cross-Reference to the "Numbered item" does display the information and you CAN read that quite normally.

    Since they can be bookmarked an alternate approach might be to bookmark all the fields, do a global List.ConvertNumbersToText, read the information via the Bookmarks and then a single Undo.


    Cindy Meister, Office Developer/Word MVP, <a href="http://blogs.msmvps.com/wordmeister"> my blog</a>

    • Marked as answer by nora.sailer Wednesday, January 13, 2016 5:34 PM
    Monday, January 11, 2016 5:39 PM
    Moderator

All replies

  • Please provide us with details on the ListNum field code and how you're using it. This is not enough information to repro. Also, which version of Word is involved?

    Cindy Meister, Office Developer/Word MVP, <a href="http://blogs.msmvps.com/wordmeister"> my blog</a>

    Friday, January 8, 2016 4:50 PM
    Moderator
  • Hi Cindy Meister

    Thanks for replying so quickly.

    It's Word 2010.

    The ListNum field code: { LISTNUM Vertragsbeilage }, Vertragsbeilage being the name of the ListNum template. I am adding those fields as follows:

    ActiveDocument.Content.Fields.Add(rangeSelection, WdFieldType.wdFieldEmpty, "LISTNUM Vertragsbeilage", false)

    These fields are needed for continuously numbered annexes in the text.

    As I said above, I can reproduce the phenomenon also with other numbered fields, such as page number, which might be easier for testing as you don't have to add a ListNum template:

    • Take a Word document
    • Start a numbered list on page 1
    • Set the ListNum field or just the page number as a field in any paragraph but the first (so you can see the difference in the result: page number would be 1, paragraph would be 2 or higher).
    • Select the page number field with the mouse
    • Execute following line of code, "selection" being the Selection of your document or application add-in:
    MessageBox.Show(selection.Range.ListFormat.ListString);
    • Or just set the breakpoint at Range.ListFormat.ListString.

    Kind regards, Nora

    Friday, January 8, 2016 5:59 PM
  • Hi Nora

    Mmmm. The Page field has absolutely nothing to do with Range.ListFormat. Selection.Range.ListFormat.ListString is going to return the numbering of the List - the paragraph.

    And I don't KNOW the logic, but I suspect that Word is going to apply that principle in a paragraph that's automatically numbered, even if the selection is a ListNum field.

    You might need to have your code check whether Selection is a ListNum field and, if it is, get that field's Result property?


    Cindy Meister, Office Developer/Word MVP, <a href="http://blogs.msmvps.com/wordmeister"> my blog</a>

    Friday, January 8, 2016 7:23 PM
    Moderator
  • Hi Cindy

    Yes, you're most probably right (as usual): Word always seems to return the ListString of the paragraph, if the paragraph is numbered, otherwise the ListString of the ListNum field.

    Ok, I tried to get the display value of the field in several ways:

    field.Code.Text returns the expected field code, so at least I know that I'm examining the right field.

    field.Result.Text: returns null

    field.Result.FormattedText.Text: returns null

    field.Unlink(); then get Text of that range or selection: returns the desired value

    So at least SOMETHING works, but I'd rather not unlink/undo several hundreds of fields. Do you know why feld.Result.Text is null? What am I missing here.

    Thanks and kind regards

    Nora

    Monday, January 11, 2016 12:47 PM
  • Hi Nora

    Haven't a clue, and I can't find anything better...

    As a workaround you might try transferring the range (copy, paste as unformatted text) to a hidden "temp" document. That way no worries about undoing or damaging the original document.

    FWIW putting it in a QUOTE field makes it invisible.

    Creating a Cross-Reference to the "Numbered item" does display the information and you CAN read that quite normally.

    Since they can be bookmarked an alternate approach might be to bookmark all the fields, do a global List.ConvertNumbersToText, read the information via the Bookmarks and then a single Undo.


    Cindy Meister, Office Developer/Word MVP, <a href="http://blogs.msmvps.com/wordmeister"> my blog</a>

    • Marked as answer by nora.sailer Wednesday, January 13, 2016 5:34 PM
    Monday, January 11, 2016 5:39 PM
    Moderator
  • Hi Cindy

    Indeed, copy/paste into a temporary throwaway document works.

    I first add the same ListNum template to that document, then copy one range after the other of the original to the temp document, each time unlinking the field in the temp document, read its text, undo the unlinking (for the subsequent numbers), ...

    If users complain about the performance, I might try your second suggestion for speed comparison, the one with the cross references. But we're talking about maximum 500 occurences, so I think we'll be fine with the first solution.

    Thanks, have a nice day

    Nora

    Wednesday, January 13, 2016 5:42 PM