none
VSTO Word - How to get specified string position throughout the word doc and Navigate on button click RRS feed

  • Question

  • Dear All

    I am using VS2010, C#.

    I want to
    detect specified string (like "%") position which presents number of
    times in word doc.

    After getting position, I want to navigate cursor on button click for respective
    position
    throughout the word doc

    Please share your thoughts

    Thanks in advance

    ZRISHI



    Thursday, October 18, 2012 4:44 AM

Answers

  • Hi ZRishi

    In this case, you'd need two "Find" steps, the first to pick up the opening << (as you've already done) and the second to pick up the closing >>. If you do that with two different Range objects, then you can use the Start position of the one Range and the End position of the other to specify the range for the bookmark. In pseudocode it would look something like this

      Word.Range rngFindStart = Document.Content.Duplicate;
      Word.Range rng FindEnd = Document.Content.Duplicate
      bool foundStart = true;
      bool foundEnd = true;
      object oCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd;
      //build a loop here; you want to loop while Found = true...
      foundStart = rngFindStart.Find.Execute(//params here);
      foundEnd = rngFindEnd.Find.Execute(//params here);
      if (foundStart && foundEnd)
      {
        rngFindStart.End = rngFindEnd.End
        Document.Bookmarks.Add("name", rngFindStart);
        rngFindStart.Collapse(ref oCollapseEnd);
        rngFindEnd.Collapse(ref oCollapseEnd);
        rngFindStart.End = Document.Content.End;
        rngFindEnd.End = Document.Content.End;
      }
      //end of loop


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, November 1, 2012 4:20 PM
    Moderator

All replies

  • Hi ZRISHI,

    Thanks for posting in the MSDN Forum.

    Hope this Range.Find Ojbect in Word can help you for this issue. You can highlight the position of the specified string by using the Find.HitHighlight method, but I haven't found there is a way in moving the cursor to the respective position.

    Best Regards,


    Leo_Gao [MSFT]
    MSDN Community Support | Feedback to us

    Friday, October 19, 2012 8:35 AM
    Moderator
  • Thanks Leo_Gao [MSFT] for your reply.

    I will check and let you know.

    Friday, October 19, 2012 8:46 AM
  • HI Leo / ZRishi

    If Range.Find.Execute is true all you need to do so SELECT the "hit" is to use the Range.Select() method.


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, October 19, 2012 1:52 PM
    Moderator
  • Thanks Cindy Meister for your reply.

    I am able to find and select specified string but still I have some req as below

    1) How to get specified char position from .doc like my lines are ex..

    ---------------------------------------------------------------

    India, #officially the Republic of India, is a country in South Asia. It is the #seventh-largest country #by area

    ---------------------------------------------------------------

    From above lines, I want to get and stors "#" char positions from .doc

    2) Move cursor on respective positions on button click.

    Kindly revert in case of any other query.

    Thanks in advance.

    ZRishi

    Tuesday, October 23, 2012 6:11 AM
  • Hi Zrishi

    I should probably mention that, from Word 2010 onwards the functionality you want to build is incorporated in Word's Navigation pane...

    You do not want to try doing this by storing the character position (index number of the character in the document). You can't be certain from one moment to the next that this index will be the same, and there are a number of hidden factors that affect what the number is vs. what Word calculates the number to be. It's in no way reliable.

    All things being equal, you should insert a bookmark at the target range (Bookmarks.Add method). You can name the bookmarks in such a way as to build a "series" through which you can cycle.

    When you click the button, you'd just select the target's Bookmark.Range. In C# something like (typing from memory): document.Bookmarks.get_item("name").Range.Select();


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, October 23, 2012 6:36 AM
    Moderator
  • ok.. I will try with this approch(document.Bookmarks.get_item("name").Range.Select();)

    Let me check this..

    Tuesday, October 23, 2012 6:47 AM
  • I am able to add bookmark by following method :

    word.Range rng = this.Application.ActiveDocument.Range(3, 5);

    document.Bookmarks.Add("BM1",rng);

    In above lines, I have set/hardcode range value like 3,5

    Can I set it dynamically / How can I set target range at run time?




    Tuesday, October 23, 2012 11:29 AM
  • I thought I typed an answer to this the other day...

    If you use Leo's Range.Find approach, the Range object will change to contain the found text. So you'd pass that Range to Bookmarks.Add.

    However, that means you need to reset the Range in order to continue the Find, otherwise you keep hitting on the same Range, over and over (and go into a endless loop). So something along these lines:

    Word.Range rngFind = Document.Content.Duplicate;
    bool found = true;
    object oCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd;
    //build a loop here; you want to loop while Found = true...
    found = rngFind.Find.Execute(//params here);
    if (found)
    {
      Document.Bookmarks.Add("name", rngFind);
      rngFind.Collapse(ref oCollapseEnd);
      rngFind.End = Document.Content.End;
    }
    //end of loop


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, October 25, 2012 8:53 PM
    Moderator
  • Thanks Cindy Meister for your reply.

    With the help of above line's and some changes, now I am able to add bookmark's for specified
    string.

    I have also specified separate name's for each bookmark’s and its working perfect now.

    After adding bookmarks, doc lines look's as bellow.

    India, [<<] officially>> the Republic of India, is a country in South Asia. It is the [<<] seventh-largest>> country [<<]by area>>

    --------------------------------------------------------------------------------------------------------

    Note - I have
    specified "<<" as findtext;

    Instead of
    above lines, can I make it as bellow??

    India, [<<officially>>] the Republic of India, is a country in South Asia. It is the [<<seventh-largest>>] country [<<by area>>]

    In one
    line, I want to bookmark the string which comes between"<<" ">>"

    Please share your thoughts.





    Friday, October 26, 2012 9:13 AM
  • Hi ZRishi

    In this case, you'd need two "Find" steps, the first to pick up the opening << (as you've already done) and the second to pick up the closing >>. If you do that with two different Range objects, then you can use the Start position of the one Range and the End position of the other to specify the range for the bookmark. In pseudocode it would look something like this

      Word.Range rngFindStart = Document.Content.Duplicate;
      Word.Range rng FindEnd = Document.Content.Duplicate
      bool foundStart = true;
      bool foundEnd = true;
      object oCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd;
      //build a loop here; you want to loop while Found = true...
      foundStart = rngFindStart.Find.Execute(//params here);
      foundEnd = rngFindEnd.Find.Execute(//params here);
      if (foundStart && foundEnd)
      {
        rngFindStart.End = rngFindEnd.End
        Document.Bookmarks.Add("name", rngFindStart);
        rngFindStart.Collapse(ref oCollapseEnd);
        rngFindEnd.Collapse(ref oCollapseEnd);
        rngFindStart.End = Document.Content.End;
        rngFindEnd.End = Document.Content.End;
      }
      //end of loop


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, November 1, 2012 4:20 PM
    Moderator