none
Range.Find() a no-no if the Document/Window is inactive? RRS feed

  • Question

  • Hi all;

    I'm getting a exception calling Range.Find() and it seems to be occuring when the document is inactive (calling in response to a ribbon menu isEnabled call on the inactive document/window). Is calling Range.Find() only allowed on the active window?

    And if so, 1. how do I determine if a Document and/or Window is active? 2. Should I then walk Paragraph objects doing the search myself?

    thanks - dave


    The future of B.I. - Raw & Uncut
    Wednesday, June 15, 2011 3:00 PM

All replies

  • Hi Dave,

    There's not a problem using a Range.Find on a document that is not in the active window, you just have to have to have stored a reference to that other document.

    For example for there are two open documents at some point in your code you'd have assigned an object variable such as "doc1" to one of the documents and "doc2" to the other. If doc2 was inactive but you wanted to search it with a range command, the command would look like doc2.range.find

    Hope this helps


    Kind Regards, Rich ... http://greatcirclelearning.com
    Wednesday, June 15, 2011 3:43 PM
  • Hi Rich;

    Ok, not due to being inactive. Any idea why I would get this error when calling it:

    System.Runtime.InteropServices.COMException occurred
     HelpLink=wdmain11.chm#41867
     Message=Command is not available
     Source=Microsoft Word
     ErrorCode=-2146819189
     StackTrace:
        at Microsoft.Office.Interop.Word.Find.Execute(Object& FindText, Object& MatchCase, Object& MatchWholeWord, Object& MatchWildcards, Object& MatchSoundsLike, Object& MatchAllWordForms, Object& Forward, Object& Wrap, Object& Format, Object& ReplaceWith, Object& Replace, Object& MatchKashida, Object& MatchDiacritics, Object& MatchAlefHamza, Object& MatchControl)
        at AutoTag2010.net.windward.autotag.word.WordTagHandler.Find(Selection selOn, Int32 startOffset, Int32 endOffset, Boolean forward, String text) in C:\src\Jenova\Dev\AutoTag\AutoTag2010\net\windward\autotag\word\WordTagHandler.cs:line 774
     InnerException: 
    


    ??? - thanks - dave


    The future of B.I. - Raw & Uncut
    Wednesday, June 15, 2011 7:33 PM
  • Hi Dave,

    No I don't know why. What are you trying to find with the Range.Find command?


    Kind Regards, Rich ... http://greatcirclelearning.com
    Thursday, June 16, 2011 10:34 AM
  • Hi Dave

    <<at AutoTag2010.net.windward.autotag.word.WordTagHandler.Find(Selection selOn, Int32 startOffset, Int32 endOffset, Boolean forward, String text)>>

    I don't know how Selection selOn is defined as data type, but my best guess would be Word.Application.Selection. And Selection can only be in the ActiveDocument/ActiveWindow. It can't be in anything else. You might need to work with the Range object, instead.


    Cindy Meister, VSTO/Word MVP
    Thursday, June 16, 2011 12:41 PM
    Moderator
  • Selection can only be in the ActiveDocument/ActiveWindow
     
    I have to disagree on that one. Every Window has a Selection; the Application Selection is the Selection in the active window, but there are (potentially) lots of other Selections. That said, of course, working with non-selected Ranges is better.
     
    I don't know the answer to Dave's problem but 'command not available' is the sort of message that is issued when Word is in some mode where the command can't be used, although I can't think of any such mode, off the top of my head, where Find can't be used, except, perhaps, when a dialog box is on display.
     

    Enjoy,
    Tony
    www.WordArticles.com
    Thursday, June 16, 2011 3:02 PM
  • Hi Tony

    <<

    Selection can only be in the ActiveDocument/ActiveWindow
     
    I have to disagree on that one. Every Window has a Selection; the Application Selection is the Selection in the active window, but there are (potentially) lots of other Selections. That said, of course, working with non-selected Ranges is better.>>
     
    Yes, but we are talking object model, here, and so there is only one Selection - that of the Application object, which will be in the active document/active window. Anything else is not available...

    Cindy Meister, VSTO/Word MVP
    Thursday, June 16, 2011 4:10 PM
    Moderator
  • No. Look at the results of this (with more than one window open!) ...
     
    For each W in Application.Windows
        debug.print w.selection.start
    next
     

    Enjoy,
    Tony
    www.WordArticles.com
    Thursday, June 16, 2011 4:18 PM
  • Hi all;

    Ok, what appears to be happening is on a WindowDeactivate event, I call ribbon menu Invalidate() which calls my code where I then look for text in the selection.

    Sometimes, and only sometimes, it then throws the exception in Find. I'm guessing that sometimes Word is in a state where it doesn't like being called when it calls that event.

    Does that make sense?

    thanks - dave


    The future of B.I. - Raw & Uncut
    Thursday, June 16, 2011 10:52 PM
  • Interesting. If you are in a WindowDeactivate Event, what are you deactivating and, possibly more relevant, what are you about to activate (I must say that, in my experience, WindowActivate/Deactivate events do not fire with 100% reliability)? What window are you trying to use the Selection from, and is it active?
     

    Enjoy,
    Tony
    www.WordArticles.com
    Friday, June 17, 2011 9:21 AM
  • Hi Tony;

    We do two things.

    1. Update the contents of our custom task panes to match the active window.
    2. Update the ribbon.

    For the ribbon update we use the selection of the window the ribbon is tied to.

    thanks - dave


    The future of B.I. - Raw & Uncut
    Friday, June 17, 2011 11:54 AM
  • Can I display my ignorance, again?
     
    The StackTrace in the abend you posted earlier in the thread shows: Microsoft.Office.Interop.Word.Find.Execute(...
     
    Where do you specify what it is you are looking in - Selection, Range, whatever?
     

    Enjoy,
    Tony
    www.WordArticles.com
    Monday, June 20, 2011 10:02 AM
  • Hi Tony;

    I'm calling Selection.Duplicate to get a Range and then working from that. And it works fine most of the time. But sometimes it throws the exception.

    And this only occurs on a activate/deactivate where I then call menu invalidate whcih then calls back to my code. This all occurs in the thread that the (de)activate event comes in on.

    I've figured out how we don't need to invalidate the menu on a deactivate, but we do need it on an activate.

    thanks - dave


    The future of B.I. - Raw & Uncut
    Monday, June 20, 2011 3:48 PM
  • Hi Dave,

    Have you resolved your problem? We are doing the research about your problem. There might be some delay about the response. Appreciate your patience.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, June 23, 2011 9:13 AM
  • Hi;

    Nope, no solution yet. I'm hoping you find one.

    thanks - dave


    The future of B.I. - Raw & Uncut
    Thursday, June 23, 2011 2:15 PM
  • Hi Dave,

     

    Can you repro this behavior using just VBA and custom ribbon/Backstage XML or does it only repro in your COM addin?

     

    If it repros in VBA, can you please post the necessary VBA code/XML and required repro steps for a simple repro of the issue?

     

    Regards,

    Bill


    Bill Go - MSFT
    Friday, June 24, 2011 4:55 PM
    Answerer
  • Hi Bill;

    Unfortunately it happens very rarely and only under our AddIn. I posted hoping someone else had hit this and figured it out. I don't see any way to get an example that will let you guys dive into it.

    thanks - dave


    The future of B.I. - Raw & Uncut
    Friday, June 24, 2011 7:52 PM
  • Hi David,

    Thanks for your reply. The only other option would be to have you open a regular support case.

    http://support.microsoft.com/select/?target=assistance

    Regards,

    Bill

     


    Bill Go - MSFT
    Monday, July 4, 2011 2:38 AM
    Answerer
  • Hi;

    I found out how to have it occur 100% of the time. Have an AddIn open a new Word document. Break in the debugger just before you call open so that another app is the active app.

    Then in the Window Activate event call Find - you'll get this problem 100% of the time (at least in my tests I do).

    thanks - dave


    The future of B.I. - Raw & Uncut
    Sunday, July 17, 2011 10:08 PM
  • Hi;

    Asking again, any way to address this?

    thanks - dave


    The future of B.I. - Raw & Uncut
    Monday, July 25, 2011 4:38 PM
  • I posted a way below to get this to occur 100% of the time - any chance of a work-around?

    thanks - dave


    The future of B.I. - Raw & Uncut
    Tuesday, August 16, 2011 7:35 PM
  • Hi Dave,
     
    This is probably just unrelated co-incidence (and your reminder here just timely) but I have been having a similar problem in another situation, that I haven't yet resolved. In VBA in Word 2007 SP2 (I haven't checked anywhere else yet), I have a SaveAs followed by ActiveWindow.whatever and, when stepping through the code, ActiveWindow is Nothing and the command fails although it appears to work when running normally (I stumbled over this when looking at another problem in the same code and single stepping through it - this wasn't what I was looking at at all!). It makes no sense to me at the moment and I haven't dug into it, but it seems like something just isn't joined up behind the scenes.
     

    Enjoy,
    Tony
    www.WordArticles.com
    Thursday, August 18, 2011 12:23 PM