none
Getting the current index of a Office.Interop.Word.Field-object

    Question

  • Hello

    I want to retrieve the current count (not amount of Fields that is created by "test", but the highest number of the fields with the name "test"), ehm...

      text = "SEQ test \\r " + StartNumber;
    //The user can reset the starting number of test at any given time.

    Then I add the field to the document as such:

    object text = "SEQ test \\n" //Or with \\r option seen above, for reseting... ... Word.Field fieldAdded = selection.Fields.Add(selection.Range, ref empty, ref text, ref isTrue); MessageBox(fieldAdded.Index.ToString()); //This displays the last index, as in; the index of the newly added field.

    Now I want to retrieve the number of the last added field of type "SEQ test", take this image for example:

    http://s18.postimage.org/9u8l7gtbr/Capture.png

    The user has here added [through a WinForm] a starting number 25, so the first selection.Fields.Add is done with the reset option to 25. Then the user has called on the same function twice, hence two more rows.

    Now I want to retreive the last number, 27, based on the Selection.Fields, or wherever the fields I've added are stored...

    I've tried:

    Selection.Fields.Add(selection.Range, ref empty, ref text, ref isTrue);//Adding at least one field
    
    
    Selection.Range.Fields.PreviousField()/NextField()
    //Returns null, always.
    
    
    
    foreach(Office.Interop.Core.Field f in Selection.Range.Fields)
    {
        //Never reached, Selection.Range.Fields is always empty
        //Even though I try to add a Field in the line before
    }
    
    
    Selection.Range.Fields[0]/[1] also returns null


    Word.Field field = application.Selection.Range.Fields["test"];
    int TheIndex = field.Index; //Or get the text, and then split it to find the value 27...


    So, in the image, how can I retrieve the "27" through the Selection.Fields-object-list...?


    • Edited by colaohye Wednesday, December 12, 2012 12:30 PM
    Wednesday, December 12, 2012 10:02 AM

Answers

  • Hi colaohye

    I'm not sure I understand the logic of your application... Is there a reason you use Selection rather than a Range object? My inclination would be to do something like this:

      int fieldCounter = 0;
      Word.Range rngGetFields= theDoc.Content;
      //If you want to have the fields up to the correct selection point in the document
      //Then we set the end point of the Range to the end of the current selection
      rngGetFields.End = Selection.Range.End;
      foreach(Office.Interop.Core.Field f in rngGetFields.Fields)
      {
        if (fld.Type = Word.WdFieldType.wdFieldSeq && fld.Code.Text.Contains("test"))

       {
          fieldCounter++;
       }
       }


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, December 12, 2012 1:44 PM
  • Hi colaohye

    Glad my suggestion turned out to be what you're looking for :-)

    My concern, mainly, was this:
      foreach(Office.Interop.Core.Field f in Selection.Range.Fields)

    Selection will only give you what's SELECTED in the document. Theoretically, what you have should work, but I'm guessing that in your scenario the Selection could be anything (whatever the user is currently doing) or is a blinking marker on the document surface, rather than a selection extending from the beginning of the document to the desired end-point. That would explain why you wouldn't get any fields, at all...


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, December 12, 2012 3:09 PM

All replies

  • Hi colaohye

    I'm not sure I understand the logic of your application... Is there a reason you use Selection rather than a Range object? My inclination would be to do something like this:

      int fieldCounter = 0;
      Word.Range rngGetFields= theDoc.Content;
      //If you want to have the fields up to the correct selection point in the document
      //Then we set the end point of the Range to the end of the current selection
      rngGetFields.End = Selection.Range.End;
      foreach(Office.Interop.Core.Field f in rngGetFields.Fields)
      {
        if (fld.Type = Word.WdFieldType.wdFieldSeq && fld.Code.Text.Contains("test"))

       {
          fieldCounter++;
       }
       }


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, December 12, 2012 1:44 PM
  • You understood perfectly well, or, at least you gave me the answer I was hoping for, so thats how it is done, grabbing the range.

    Reason for using Selection? No, just that Selection worked so far, so never thought of grabbing the range...

    And the fact that I added a Field to Selection.Range.Fields instead of Selection.Fields... Did not see it, or maybe it does not matter, maybe its the same Fields-list, oh well...


      //If you want to have the fields up to the correct selection point in the document
      //Then we set the end point of the Range to the end of the current selection

    Yes, indeed. :)

    thanks a lot!

    Edit: Was in a hurry when I posted the reply, youre actually just counting through up till the position of the last field, well, ... I wanted the number "27", when the document actually contains only 3 fields, as seen in the picture in first post.

    But I see now, that I can get it by splitting the field.Code.Split(" ")[index].... So, yes, thanks for pointing me in the right direction! :)

    • Edited by colaohye Tuesday, December 18, 2012 9:51 AM
    Wednesday, December 12, 2012 2:54 PM
  • Hi colaohye

    Glad my suggestion turned out to be what you're looking for :-)

    My concern, mainly, was this:
      foreach(Office.Interop.Core.Field f in Selection.Range.Fields)

    Selection will only give you what's SELECTED in the document. Theoretically, what you have should work, but I'm guessing that in your scenario the Selection could be anything (whatever the user is currently doing) or is a blinking marker on the document surface, rather than a selection extending from the beginning of the document to the desired end-point. That would explain why you wouldn't get any fields, at all...


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, December 12, 2012 3:09 PM