none
easy mapping from ActiveWindows.Selection to ActiveDocument RRS feed

  • Question

  • Hello All,

    Can someone point me to a simple way to map from:

    ...Application.ActiveWindow.Selection.Sentences Collection
                 TO
    ...Application.ActiveDocument.Sentences Collection

    I want to process sentences via the same code, regardless of selectiontype:  InsertionPoint using ActiveDocument or Selection using ActiveWindow.Selection.

    Thanks billwa992

    Thursday, September 8, 2011 3:28 PM

Answers

  • Hi Bill

    More likely an error in my understanding :-) I think I was seeing "ActiveDocument.Selection", yesterday... (long day, nasty project)

    How about something along these lines:

      Word.Range rngSelection = Application.Selection.Range;
      Word.Range rngDocument = Application.ActiveDocument.Content;
      DoSomethingWithSentences(rngSelection) //or rngDocument

      private void DoSomethingWithSentences(Word.Range rng){ rng.Sentences.Count }

    In both cases, then you're working with a Range object, so both scenarios should be able to leverage the same code...

    EDIT: <duh>I don't work with Sentences much, sorry for the lapse in logic. The Sentences property returns a RANGE object. So you should be able to pass Whichever.Sentences to the same method. That method signature needs to have a Range argument (as above).


    Cindy Meister, VSTO/Word MVP
    Friday, September 9, 2011 9:12 AM
    Moderator

All replies

  • Hi Bill

    Eh, I think you mean:

    Application.Selection

    ActiveDocument is always going to be the document "on top" when you use Application.Selection, which is why there's no Selection property for the Document object.


    Cindy Meister, VSTO/Word MVP
    Thursday, September 8, 2011 3:36 PM
    Moderator
  • Cindy,

    Um, No, I think I mean ActiveDocument, as I only initiate my AddIn functionality on the Document Object "on top".

    And I want to process the Sentences Collection.  if nothing is selected (I.E. selectiontype is InsertionPoint), I process the whole document using ActiveDocument.Sentences collection.  if the user has selected a portion of the document (I.E. all the other selectiontypes) then I want to process only that portion of the document using ActiveWindow.Selection.Sentences Collection.  I'm looking for a simple way to map the selection's Sentences Collection, back to the document's Sentences Collection so I can use common code.  Does this make sense??  Or where is the error in my understanding.

    Thanks for the very quick response.

    billwa992


    • Edited by billwa992 Thursday, September 8, 2011 6:36 PM
    Thursday, September 8, 2011 3:56 PM
  • Hi Bill

    More likely an error in my understanding :-) I think I was seeing "ActiveDocument.Selection", yesterday... (long day, nasty project)

    How about something along these lines:

      Word.Range rngSelection = Application.Selection.Range;
      Word.Range rngDocument = Application.ActiveDocument.Content;
      DoSomethingWithSentences(rngSelection) //or rngDocument

      private void DoSomethingWithSentences(Word.Range rng){ rng.Sentences.Count }

    In both cases, then you're working with a Range object, so both scenarios should be able to leverage the same code...

    EDIT: <duh>I don't work with Sentences much, sorry for the lapse in logic. The Sentences property returns a RANGE object. So you should be able to pass Whichever.Sentences to the same method. That method signature needs to have a Range argument (as above).


    Cindy Meister, VSTO/Word MVP
    Friday, September 9, 2011 9:12 AM
    Moderator