Reading a document from bottom









up vote
1
down vote

favorite












At very begining these two blue words are red highlighted. When I click button1(Next),it turn first red word to blue and onclicking again the next one red to blue and so on. Now if I click the button2(prev) then it should turn again red but from the back side or you can say the last highlighted oneI have created a add-in for MS word. I have two buttons. Click on first move me forward by highlighting a range of words. On second button click I want to go to the previous highlighted word. Can anybody help me in second button functionality. On button click one I have this code working fine.Now how to go the previously highlighted word range on button2 click??



private void adxRibbonButton1_OnClick(object sender, IRibbonControl control, bool pressed)

object missing = System.Type.Missing;
Word.Document document = WordApp.ActiveDocument;
foreach(Word.Range docRange in document.Words)

if(docRange.HighlightColorIndex.Equals(Microsoft.Office.Interop.Word.WdColorIndex.wdRed))

docRange.HighlightColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdBlue;
docRange.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdWhite;
break;













share|improve this question



























    up vote
    1
    down vote

    favorite












    At very begining these two blue words are red highlighted. When I click button1(Next),it turn first red word to blue and onclicking again the next one red to blue and so on. Now if I click the button2(prev) then it should turn again red but from the back side or you can say the last highlighted oneI have created a add-in for MS word. I have two buttons. Click on first move me forward by highlighting a range of words. On second button click I want to go to the previous highlighted word. Can anybody help me in second button functionality. On button click one I have this code working fine.Now how to go the previously highlighted word range on button2 click??



    private void adxRibbonButton1_OnClick(object sender, IRibbonControl control, bool pressed)

    object missing = System.Type.Missing;
    Word.Document document = WordApp.ActiveDocument;
    foreach(Word.Range docRange in document.Words)

    if(docRange.HighlightColorIndex.Equals(Microsoft.Office.Interop.Word.WdColorIndex.wdRed))

    docRange.HighlightColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdBlue;
    docRange.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdWhite;
    break;













    share|improve this question

























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      At very begining these two blue words are red highlighted. When I click button1(Next),it turn first red word to blue and onclicking again the next one red to blue and so on. Now if I click the button2(prev) then it should turn again red but from the back side or you can say the last highlighted oneI have created a add-in for MS word. I have two buttons. Click on first move me forward by highlighting a range of words. On second button click I want to go to the previous highlighted word. Can anybody help me in second button functionality. On button click one I have this code working fine.Now how to go the previously highlighted word range on button2 click??



      private void adxRibbonButton1_OnClick(object sender, IRibbonControl control, bool pressed)

      object missing = System.Type.Missing;
      Word.Document document = WordApp.ActiveDocument;
      foreach(Word.Range docRange in document.Words)

      if(docRange.HighlightColorIndex.Equals(Microsoft.Office.Interop.Word.WdColorIndex.wdRed))

      docRange.HighlightColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdBlue;
      docRange.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdWhite;
      break;













      share|improve this question















      At very begining these two blue words are red highlighted. When I click button1(Next),it turn first red word to blue and onclicking again the next one red to blue and so on. Now if I click the button2(prev) then it should turn again red but from the back side or you can say the last highlighted oneI have created a add-in for MS word. I have two buttons. Click on first move me forward by highlighting a range of words. On second button click I want to go to the previous highlighted word. Can anybody help me in second button functionality. On button click one I have this code working fine.Now how to go the previously highlighted word range on button2 click??



      private void adxRibbonButton1_OnClick(object sender, IRibbonControl control, bool pressed)

      object missing = System.Type.Missing;
      Word.Document document = WordApp.ActiveDocument;
      foreach(Word.Range docRange in document.Words)

      if(docRange.HighlightColorIndex.Equals(Microsoft.Office.Interop.Word.WdColorIndex.wdRed))

      docRange.HighlightColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdBlue;
      docRange.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdWhite;
      break;










      c# ms-word






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 8 at 13:21

























      asked Nov 6 at 6:56









      Amar Malik

      196




      196






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          There's more than one way to approach this:



          1. Use Word's built-in Find to search backwards in the document for the first instance of the changed highlighting.



          2. Set two bookmarks, one for the current position of the code in the question and one for the previous position. The code sample below is for this variation.



            string CurrentBkm = "_bkmCurrent";
            string LastBkm= "_bkmLast";

            if(docRange.HighlightColorIndex.Equals(Microsoft.Office.Interop.Word.WdColorIndex.wdRed))
            {
            docRange.HighlightColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdBlue;
            docRange.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdWhite;
            if (document.Bookmarks.Exists(CurrentBkm))

            document.Bookmarks.Add(LastBkm, document.Bookmarks[CurrentBkm].Range.Duplicate);

            document.Bookmarks.Add(CurrentBkm, docRange);
            break;


          The code for button2 simply goes to the bookmark "_bkmLast":



          string LastBkm= "_bkmLast";
          document.Bookmarks[LastBkm].Range.Select();


          Note that the bookmark name starts with an underscore _. This hides the bookmark in the Word UI so that it won't irritate the user in case the application settings show bookmark non-printing characters.



          Note, also, that the code in the question could also work with Word's built-in Find functionality to search the formatting. This would almost certainly be more efficient than "walking" each word in the document and testing its highlight formatting. If you were to change your code to use Find the solution I provide with the bookmarks would still work.






          share|improve this answer






















          • yes yes,When I click three times, clicking button2 should take me to the 2nd click of button1.......and on four time click of button1. the button2 click should take me to 3rd click of button1. Means one step back
            – Amar Malik
            2 days ago










          • OK @AmarMalik Sorry for the misunderstanding - I've adjusted my Answer.
            – Cindy Meister
            2 days ago










          • sorry for the inconvenience,here it seems like button2 functionality will work for only one click . If I press button2 more than one time it should go to previous to previous one. Just opposite of button1 functionality Eg. button1_click->1,2,3,4,5 button2_click->4,3,2,1 And I am aware of find but don't how to move backward with the help of this
            – Amar Malik
            2 days ago











          • In both the question and the comment you say "one step back". That's what my answer does.
            – Cindy Meister
            2 days ago










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53167061%2freading-a-document-from-bottom%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote













          There's more than one way to approach this:



          1. Use Word's built-in Find to search backwards in the document for the first instance of the changed highlighting.



          2. Set two bookmarks, one for the current position of the code in the question and one for the previous position. The code sample below is for this variation.



            string CurrentBkm = "_bkmCurrent";
            string LastBkm= "_bkmLast";

            if(docRange.HighlightColorIndex.Equals(Microsoft.Office.Interop.Word.WdColorIndex.wdRed))
            {
            docRange.HighlightColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdBlue;
            docRange.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdWhite;
            if (document.Bookmarks.Exists(CurrentBkm))

            document.Bookmarks.Add(LastBkm, document.Bookmarks[CurrentBkm].Range.Duplicate);

            document.Bookmarks.Add(CurrentBkm, docRange);
            break;


          The code for button2 simply goes to the bookmark "_bkmLast":



          string LastBkm= "_bkmLast";
          document.Bookmarks[LastBkm].Range.Select();


          Note that the bookmark name starts with an underscore _. This hides the bookmark in the Word UI so that it won't irritate the user in case the application settings show bookmark non-printing characters.



          Note, also, that the code in the question could also work with Word's built-in Find functionality to search the formatting. This would almost certainly be more efficient than "walking" each word in the document and testing its highlight formatting. If you were to change your code to use Find the solution I provide with the bookmarks would still work.






          share|improve this answer






















          • yes yes,When I click three times, clicking button2 should take me to the 2nd click of button1.......and on four time click of button1. the button2 click should take me to 3rd click of button1. Means one step back
            – Amar Malik
            2 days ago










          • OK @AmarMalik Sorry for the misunderstanding - I've adjusted my Answer.
            – Cindy Meister
            2 days ago










          • sorry for the inconvenience,here it seems like button2 functionality will work for only one click . If I press button2 more than one time it should go to previous to previous one. Just opposite of button1 functionality Eg. button1_click->1,2,3,4,5 button2_click->4,3,2,1 And I am aware of find but don't how to move backward with the help of this
            – Amar Malik
            2 days ago











          • In both the question and the comment you say "one step back". That's what my answer does.
            – Cindy Meister
            2 days ago














          up vote
          0
          down vote













          There's more than one way to approach this:



          1. Use Word's built-in Find to search backwards in the document for the first instance of the changed highlighting.



          2. Set two bookmarks, one for the current position of the code in the question and one for the previous position. The code sample below is for this variation.



            string CurrentBkm = "_bkmCurrent";
            string LastBkm= "_bkmLast";

            if(docRange.HighlightColorIndex.Equals(Microsoft.Office.Interop.Word.WdColorIndex.wdRed))
            {
            docRange.HighlightColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdBlue;
            docRange.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdWhite;
            if (document.Bookmarks.Exists(CurrentBkm))

            document.Bookmarks.Add(LastBkm, document.Bookmarks[CurrentBkm].Range.Duplicate);

            document.Bookmarks.Add(CurrentBkm, docRange);
            break;


          The code for button2 simply goes to the bookmark "_bkmLast":



          string LastBkm= "_bkmLast";
          document.Bookmarks[LastBkm].Range.Select();


          Note that the bookmark name starts with an underscore _. This hides the bookmark in the Word UI so that it won't irritate the user in case the application settings show bookmark non-printing characters.



          Note, also, that the code in the question could also work with Word's built-in Find functionality to search the formatting. This would almost certainly be more efficient than "walking" each word in the document and testing its highlight formatting. If you were to change your code to use Find the solution I provide with the bookmarks would still work.






          share|improve this answer






















          • yes yes,When I click three times, clicking button2 should take me to the 2nd click of button1.......and on four time click of button1. the button2 click should take me to 3rd click of button1. Means one step back
            – Amar Malik
            2 days ago










          • OK @AmarMalik Sorry for the misunderstanding - I've adjusted my Answer.
            – Cindy Meister
            2 days ago










          • sorry for the inconvenience,here it seems like button2 functionality will work for only one click . If I press button2 more than one time it should go to previous to previous one. Just opposite of button1 functionality Eg. button1_click->1,2,3,4,5 button2_click->4,3,2,1 And I am aware of find but don't how to move backward with the help of this
            – Amar Malik
            2 days ago











          • In both the question and the comment you say "one step back". That's what my answer does.
            – Cindy Meister
            2 days ago












          up vote
          0
          down vote










          up vote
          0
          down vote









          There's more than one way to approach this:



          1. Use Word's built-in Find to search backwards in the document for the first instance of the changed highlighting.



          2. Set two bookmarks, one for the current position of the code in the question and one for the previous position. The code sample below is for this variation.



            string CurrentBkm = "_bkmCurrent";
            string LastBkm= "_bkmLast";

            if(docRange.HighlightColorIndex.Equals(Microsoft.Office.Interop.Word.WdColorIndex.wdRed))
            {
            docRange.HighlightColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdBlue;
            docRange.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdWhite;
            if (document.Bookmarks.Exists(CurrentBkm))

            document.Bookmarks.Add(LastBkm, document.Bookmarks[CurrentBkm].Range.Duplicate);

            document.Bookmarks.Add(CurrentBkm, docRange);
            break;


          The code for button2 simply goes to the bookmark "_bkmLast":



          string LastBkm= "_bkmLast";
          document.Bookmarks[LastBkm].Range.Select();


          Note that the bookmark name starts with an underscore _. This hides the bookmark in the Word UI so that it won't irritate the user in case the application settings show bookmark non-printing characters.



          Note, also, that the code in the question could also work with Word's built-in Find functionality to search the formatting. This would almost certainly be more efficient than "walking" each word in the document and testing its highlight formatting. If you were to change your code to use Find the solution I provide with the bookmarks would still work.






          share|improve this answer














          There's more than one way to approach this:



          1. Use Word's built-in Find to search backwards in the document for the first instance of the changed highlighting.



          2. Set two bookmarks, one for the current position of the code in the question and one for the previous position. The code sample below is for this variation.



            string CurrentBkm = "_bkmCurrent";
            string LastBkm= "_bkmLast";

            if(docRange.HighlightColorIndex.Equals(Microsoft.Office.Interop.Word.WdColorIndex.wdRed))
            {
            docRange.HighlightColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdBlue;
            docRange.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdWhite;
            if (document.Bookmarks.Exists(CurrentBkm))

            document.Bookmarks.Add(LastBkm, document.Bookmarks[CurrentBkm].Range.Duplicate);

            document.Bookmarks.Add(CurrentBkm, docRange);
            break;


          The code for button2 simply goes to the bookmark "_bkmLast":



          string LastBkm= "_bkmLast";
          document.Bookmarks[LastBkm].Range.Select();


          Note that the bookmark name starts with an underscore _. This hides the bookmark in the Word UI so that it won't irritate the user in case the application settings show bookmark non-printing characters.



          Note, also, that the code in the question could also work with Word's built-in Find functionality to search the formatting. This would almost certainly be more efficient than "walking" each word in the document and testing its highlight formatting. If you were to change your code to use Find the solution I provide with the bookmarks would still work.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 2 days ago

























          answered Nov 6 at 18:45









          Cindy Meister

          12.9k101934




          12.9k101934











          • yes yes,When I click three times, clicking button2 should take me to the 2nd click of button1.......and on four time click of button1. the button2 click should take me to 3rd click of button1. Means one step back
            – Amar Malik
            2 days ago










          • OK @AmarMalik Sorry for the misunderstanding - I've adjusted my Answer.
            – Cindy Meister
            2 days ago










          • sorry for the inconvenience,here it seems like button2 functionality will work for only one click . If I press button2 more than one time it should go to previous to previous one. Just opposite of button1 functionality Eg. button1_click->1,2,3,4,5 button2_click->4,3,2,1 And I am aware of find but don't how to move backward with the help of this
            – Amar Malik
            2 days ago











          • In both the question and the comment you say "one step back". That's what my answer does.
            – Cindy Meister
            2 days ago
















          • yes yes,When I click three times, clicking button2 should take me to the 2nd click of button1.......and on four time click of button1. the button2 click should take me to 3rd click of button1. Means one step back
            – Amar Malik
            2 days ago










          • OK @AmarMalik Sorry for the misunderstanding - I've adjusted my Answer.
            – Cindy Meister
            2 days ago










          • sorry for the inconvenience,here it seems like button2 functionality will work for only one click . If I press button2 more than one time it should go to previous to previous one. Just opposite of button1 functionality Eg. button1_click->1,2,3,4,5 button2_click->4,3,2,1 And I am aware of find but don't how to move backward with the help of this
            – Amar Malik
            2 days ago











          • In both the question and the comment you say "one step back". That's what my answer does.
            – Cindy Meister
            2 days ago















          yes yes,When I click three times, clicking button2 should take me to the 2nd click of button1.......and on four time click of button1. the button2 click should take me to 3rd click of button1. Means one step back
          – Amar Malik
          2 days ago




          yes yes,When I click three times, clicking button2 should take me to the 2nd click of button1.......and on four time click of button1. the button2 click should take me to 3rd click of button1. Means one step back
          – Amar Malik
          2 days ago












          OK @AmarMalik Sorry for the misunderstanding - I've adjusted my Answer.
          – Cindy Meister
          2 days ago




          OK @AmarMalik Sorry for the misunderstanding - I've adjusted my Answer.
          – Cindy Meister
          2 days ago












          sorry for the inconvenience,here it seems like button2 functionality will work for only one click . If I press button2 more than one time it should go to previous to previous one. Just opposite of button1 functionality Eg. button1_click->1,2,3,4,5 button2_click->4,3,2,1 And I am aware of find but don't how to move backward with the help of this
          – Amar Malik
          2 days ago





          sorry for the inconvenience,here it seems like button2 functionality will work for only one click . If I press button2 more than one time it should go to previous to previous one. Just opposite of button1 functionality Eg. button1_click->1,2,3,4,5 button2_click->4,3,2,1 And I am aware of find but don't how to move backward with the help of this
          – Amar Malik
          2 days ago













          In both the question and the comment you say "one step back". That's what my answer does.
          – Cindy Meister
          2 days ago




          In both the question and the comment you say "one step back". That's what my answer does.
          – Cindy Meister
          2 days ago

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53167061%2freading-a-document-from-bottom%23new-answer', 'question_page');

          );

          Post as a guest














































































          Popular posts from this blog

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo