Reading a document from bottom
up vote
1
down vote
favorite
I 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
add a comment |
up vote
1
down vote
favorite
I 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
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I 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
I 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
c# ms-word
edited Nov 8 at 13:21
asked Nov 6 at 6:56
Amar Malik
196
196
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
There's more than one way to approach this:
Use Word's built-in
Find
to search backwards in the document for the first instance of the changed highlighting.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.
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
add a comment |
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:
Use Word's built-in
Find
to search backwards in the document for the first instance of the changed highlighting.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.
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
add a comment |
up vote
0
down vote
There's more than one way to approach this:
Use Word's built-in
Find
to search backwards in the document for the first instance of the changed highlighting.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.
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
add a comment |
up vote
0
down vote
up vote
0
down vote
There's more than one way to approach this:
Use Word's built-in
Find
to search backwards in the document for the first instance of the changed highlighting.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.
There's more than one way to approach this:
Use Word's built-in
Find
to search backwards in the document for the first instance of the changed highlighting.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.
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
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password