0

This is weird. Why does Visio throw exception?

Scenario:

  1. Open new instance of Visio.
  2. Press CTRL+N for a blank new document.
  3. Goto VB editor.
  4. Open the default "ThisDocument" code file.
  5. Paste the following code.

    Sub test()
        Application.ActiveWindow.SelectAll
    End Sub
    
  6. Execute the subroutine "test".

You will observe this exception for code line "Application.ActiveWindow.SelectAll":

---------------------------
Microsoft Visual Basic for Applications
---------------------------
Run-time error '-2032465766 (86db089a)':



Requested operation is presently disabled.
---------------------------
OK   Help   
---------------------------

Does anyone know why?

Andy G
  • 19,232
  • 5
  • 47
  • 69
Nayan
  • 3,092
  • 25
  • 34

3 Answers3

1

The error "Requested operation is presently disabled" means literally this - the requested operation is disabled (in menu) at the moment (because it makes no sense).

In your case, you can't do "Select all" because there is nothing to select (you have no shapes). So the command "select all" is disabled.

If you had some shapes on the drawing, this code would run just fine.

Nikolay
  • 10,752
  • 2
  • 23
  • 51
  • "you can't do "Select all" because there is nothing to select (you have no shapes). So the command "select all" is disabled." I disagree, so some part. If there is nothing to select, a good designed app should not throw exception, rather selection collection should be left empty. I am begining to think this is unrequired *feature*. – Nayan Aug 27 '13 at 09:41
0

This procedure doesn't belong in the ThisDocument module, which is intended for Document events. Right-click ThisDocument in the Project Explorer on the left and choose Insert Module, cut and paste the code in here.

If you add one or more shapes to the document and click to select one of them before running your code then it works. This is because previously there was no ActiveWindow, which was the reason for the error. Selecting all shapes is disabled if there is no active window.

Andy G
  • 19,232
  • 5
  • 47
  • 69
  • "If you add one or more shapes to the document" - my question is - why is it required to have any object in document, or else Visio will throw exception? – Nayan Aug 27 '13 at 11:19
  • If nothing is selected on the document then there is no `ActiveWindow` and SelectAll selects all *shapes*. – Andy G Aug 27 '13 at 11:24
  • ?? `ActiveWindow` depends on selected objects? – Nayan Aug 27 '13 at 11:36
  • Yes. If there is nothing selected in it.. how can it be active? – Andy G Aug 27 '13 at 11:45
  • What's the relationship between them? Can you share any link to documentation? – Nayan Aug 27 '13 at 12:57
  • [ActiveWindow](http://msdn.microsoft.com/en-us/library/office/ff766851(v=office.14).aspx). That page doesn't define the term Active though. [This link](http://www.webopedia.com/TERM/A/active.html) defines it as "the window currently receiving mouse and keyboard events". – Andy G Aug 27 '13 at 13:04
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/36351/discussion-between-nayan-and-andy-g) – Nayan Aug 27 '13 at 16:41
  • @Nayan No thanks, there is nothing really to discuss. This is how the Visio Object Model works and I don't think Microsoft are about to change their mind any time soon. – Andy G Aug 27 '13 at 18:54
0

I am feeling that Visio throws exception by design, in case you try to run select operation via code when there is no object in document.

I hope this change in future versions.

Nayan
  • 3,092
  • 25
  • 34