none
How to select text word by word in MS Word document after click on button on Task Pane RRS feed

  • Question

  • I am working on Microsoft Word add-in in C#. After click on button on Task Pane I want to select first word. After second click I want to select next word and deselect previous word,  and so on ... At the same time I want to set this selected word to label in Task Pane. How can I do this?
    Sunday, April 6, 2014 1:21 PM

Answers

  • Hello Lenkita,

    You can use the following code for selecting a specified by the i parameter word:

    ActiveDocument.Words[i].Select 

    Word display each document in a different document frame window. When you create a custom task pane for these applications, the custom task pane is associated only with a specific document. If the user opens a different document, the custom task pane is hidden until the earlier document is visible again.

    If you want to display a custom task pane with multiple documents, create a new instance of the custom task pane when the user creates a new document or opens an existing document. To do this, handle events that are raised when a document is created or opened, and then create the task pane in the event handlers. You can also handle document events to hide or display task panes depending on which document is visible.

    To associate the task pane with a specific document window, use the CustomTaskPaneCollection.Add(UserControl, String, Object) method to create the task pane, and pass a Microsoft.Office.Interop.Word.Window to the window parameter. Please read more about this in the Managing Task Panes in Multiple Word and InfoPath Documents article in MSDN.  

    • Marked as answer by Lenkita Monday, April 7, 2014 6:52 AM
    Sunday, April 6, 2014 2:33 PM

All replies

  • Hello Lenkita,

    You can use the following code for selecting a specified by the i parameter word:

    ActiveDocument.Words[i].Select 

    Word display each document in a different document frame window. When you create a custom task pane for these applications, the custom task pane is associated only with a specific document. If the user opens a different document, the custom task pane is hidden until the earlier document is visible again.

    If you want to display a custom task pane with multiple documents, create a new instance of the custom task pane when the user creates a new document or opens an existing document. To do this, handle events that are raised when a document is created or opened, and then create the task pane in the event handlers. You can also handle document events to hide or display task panes depending on which document is visible.

    To associate the task pane with a specific document window, use the CustomTaskPaneCollection.Add(UserControl, String, Object) method to create the task pane, and pass a Microsoft.Office.Interop.Word.Window to the window parameter. Please read more about this in the Managing Task Panes in Multiple Word and InfoPath Documents article in MSDN.  

    • Marked as answer by Lenkita Monday, April 7, 2014 6:52 AM
    Sunday, April 6, 2014 2:33 PM
  • Given that you have a word selected, you can move the selection to the next word with:

    Selection.Words.Last.Next.Words.First.Select

    Do note that what Word VBA considers a 'word' is not necessarily the same as you might consider a word - commas, etc. get treated as 'words' in their own right.


    Cheers
    Paul Edstein
    [MS MVP - Word]


    • Edited by macropodMVP Monday, April 7, 2014 4:18 AM Clarification
    Monday, April 7, 2014 4:16 AM
  • Thank you very much.

    I know, 'word' can be for example comma, but is there a way how to check if value from Word collection is really word (not comma or something else)?

    Monday, April 7, 2014 6:59 AM
  • I know, 'word' can be for example comma, but is there a way how to check if value from Word collection is really word (not comma or something else)?

    Only by testing what it contains. For example, to skip over such characters you might use something like:

    Sub Demo()
    Selection.Words.Last.Next.Words.First.Select
    While Trim(Selection.Words.First.Text) Like _
      "[" & Chr(33) & "-" & Chr(47) & Chr(58) & "-" & Chr(63) _
      & Chr(91) & "-" & Chr(96) & Chr(123) & "-" & Chr(255) & "]"
      Selection.Words.Last.Next.Words.First.Select
    Wend
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Monday, April 7, 2014 7:25 AM
  • It would be possible to write this code via Regex in C#, please?
    Monday, April 7, 2014 7:34 AM
  • Sorry, but I don't know C#.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Monday, April 7, 2014 7:58 AM
  • I should have one more question. I select first word in document with this code:

    ActiveDocument.Words[1].Select; 

    And with this code I select first word of selected text:

    Globals.ThisAddIn.Application.Selection.Words[1].Select();

    But I want to select word which follows after first word in selected text (second code). I know, the index of first word in selected text is 1. Is there way, how to find index of first word in selected text but in collection of all words (from all active document)?

    Monday, April 7, 2014 11:03 AM
  • I'd recommend asking RegEx related questions in the Visual C# forum instead. There you will get the most qualified feedback.

    Note, the System.Char class provides methods for checking a particular character, i.e. to check whether it is a digit, letter, special symbol, punctuation and etc.

    Monday, April 7, 2014 11:59 AM