none
How to find the near Bookmark? RRS feed

  • Question

  • Hi All,

    I am doing a VSTO Project. Supposed that I have already insert a lot of Bookmarks in my word document via C# code. Like the picture below,

    The yellow line is represented the Bookmark. Now I want to insert bookmark via Word Document WindowSelectionChange Event. When I clicked in Word Document. It will insert a bookmark. Currently, I can do this now. I want to achieve this goal. 
    1. The bookmark cannot insert between a pair of bookmarks, Let me explain this, If I want to insert a bookmark between the first one and the second one, it will display a messege box tell user that you cannot insert in this postion.
    2. But if you want to insert a bookmark in the sencode one and the third one, That's OK. You can do it.
    3. You can insert the bookmark before the first bookmark and in the end of the last bookmark.
    Simplify: I just need to get the near by two bookmarks between the insert postion.

    OK. Sorry for my bad English. I am not a native English Speaker.

    Happy New Year! Merry Christmas!
    Thursday, December 25, 2014 1:07 PM

Answers

  • Hi Ricky Yan,

    Here is the code using C# to compare the position of bookmarks:

      if (Globals.ThisAddIn.Application.Selection.Start >= Globals.ThisAddIn.Application.ActiveDocument.Bookmarks[1].Start && Globals.ThisAddIn.Application.Selection.Start <= Globals.ThisAddIn.Application.ActiveDocument.Bookmarks[2].Start)
                    MessageBox.Show("the cursor is between first and second bookmar");
                else
                    MessageBox.Show("the cursor is not between first and second bookmar");

    Hope it is helpful.

    Regards & Fei


    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.

    • Marked as answer by Terrence-Jones Wednesday, December 31, 2014 8:25 AM
    Wednesday, December 31, 2014 8:02 AM
    Moderator

All replies

  • Hi Ricky Yan,

    Based on the desscription, you want to compare the postion of the bookmarks and the postion of cursor.

    We can use Bookmark.Start and Selection.Start to get the position of the bookmarks and selection. Then we can compare the position via the starting character position of them.

    Sub testTheSelection()
    If Application.Selection.Start >= ActiveDocument.Bookmarks(1).Start And Application.Selection.Start <= ActiveDocument.Bookmarks(2).Start Then
            MsgBox "the cursor is between first and second bookmar"
    Else
            MsgBox "the cursor is not between first and second bookmar"
    End If
    
    
    End Sub
     

    Note, the bookmark order is not relative to the character position. You need to get all position of bookmarks and order them then you can get the position via the bookmark order.

    Regards & Fei


    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.

    Friday, December 26, 2014 8:35 AM
    Moderator
  • Hey man!

    Thanks for your reply. But this is not the correct answer. I am working on this now. I will post my answer later. Any way, Thank you!

    Happy New Year to You and Your lovers. :)

    Friday, December 26, 2014 8:53 AM
  • Hi Ricky Yan,

    >>Thanks for your reply. But this is not the correct answer<<

    Did I misunderstand? If yes, would you mind sharing more detail about the issue you are handling? The demo is writing with VBA, but the Word object model is same as in VSTO.

    Regards & Fei


    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.

    Friday, December 26, 2014 9:21 AM
    Moderator
  • Hi Ricky Yan,

    Here is the code using C# to compare the position of bookmarks:

      if (Globals.ThisAddIn.Application.Selection.Start >= Globals.ThisAddIn.Application.ActiveDocument.Bookmarks[1].Start && Globals.ThisAddIn.Application.Selection.Start <= Globals.ThisAddIn.Application.ActiveDocument.Bookmarks[2].Start)
                    MessageBox.Show("the cursor is between first and second bookmar");
                else
                    MessageBox.Show("the cursor is not between first and second bookmar");

    Hope it is helpful.

    Regards & Fei


    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.

    • Marked as answer by Terrence-Jones Wednesday, December 31, 2014 8:25 AM
    Wednesday, December 31, 2014 8:02 AM
    Moderator
  • Thanks, I will post my solution here later. Man!
    Wednesday, December 31, 2014 8:25 AM