We help IT Professionals succeed at work.

How do you close all shapesheet windows at once with vba (macro) code?

leslieinva
leslieinva asked
on
I'd like to close all the shapesheets with one macro so I can easily expand my view of a page whenever I need to.

I tried the "Close" example code at msdn (attached)
http://msdn2.microsoft.com/en-us/library/aa224559(office.11).aspx
but it had no effect when I ran it.
Public Sub Close_Example() 
    Dim intCounter As Integer 
    intCounter = Windows.Count 
 
    'Close all ShapeSheet windows that are open.    
    While intCounter <> 0 
        If Windows(intCounter).Type = visSheet Then 
            Windows(intCounter).Close 
            intCounter = Windows.Count 
        Else 
            intCounter = intCounter - 1 
        End If  
    Wend
End Sub

Open in new window

Comment
Watch Question

Scott HelmersVisio Consultant, Trainer, Author, and Developer
CERTIFIED EXPERT
Most Valuable Expert 2011

Commented:
Your code works properly when I run it. In fact, I can't make it not work!

Can you get any macro to run? Are you sure you have macros enabled? (dumb question but I had to ask)

If so, try opening the VB editor window and use F8 to step through the code in debug mode (if you haven't already tried this). In debug mode, when you hover over any variable you should see its current value. So, for example, if you step into the code until the first IF statement is yellow, intCounter will show the number of open windows, visSheet should show a value of 3 and Windows(intCounter).Type will show the type value for the first window. Keep pressing F8 to follow the execution through the loop as many times as there are windows.

Of course, there's the old standby -- add a message display to the code just to ensure that your macro is executing:
   MsgBox "Hello"

Author

Commented:
I do have macros that work fine (thanks to you Scott), but maybe what I'm really trying to close is the "stencil window."   I'm trying to close the window that has "Shapes" in it's title & appears over on the left when I open a lot of Visio files.  

How do you programatically close all those windows on the left so you can expand your page?  I want to add this code to a macro I have for hiding the ruler, grid, etc & making the active window fit.

Thanks!
Scott HelmersVisio Consultant, Trainer, Author, and Developer
CERTIFIED EXPERT
Most Valuable Expert 2011

Commented:
Aha! That's a window of a different color... or more specifically, of a different type.

And it isn't as simple as substituting visStencil for visSheet, as stencil windows are handled differently. I'll look into it and get back to you.


Visio Consultant, Trainer, Author, and Developer
CERTIFIED EXPERT
Most Valuable Expert 2011
Commented:
Turns out this is even easier than closing the shapesheet windows.

I started by assuming I had to do something like the code example you posted, namely iterate through a list of stencil windows and close them one-by-one. I figured out how to do that, but it left the "Shapes" window open (that's the one with the shape search box in it, and in which the stencils reside when they're open).

But if you close the shapes window itself, everything goes away, and this is all it takes:

    ActiveWindow.Windows.ItemEx("Shapes").Close

Note that "Shapes" is case sensitive, so copy the line above exactly as written.

Author

Commented:
You ROCK Scott!  That's the right code  Thanks for the explanation too.
Scott HelmersVisio Consultant, Trainer, Author, and Developer
CERTIFIED EXPERT
Most Valuable Expert 2011

Commented:
You're welcome, and thanks for the question, Leslie. A big part of the reason I do this is because it forces me to learn new things. In responding to a couple of your previous questions, I learned how to turn various UI features on or off from VBA, and now I know more about managing windows than I did before.  Some of what I learn is just interesting stuff to file away... but some of it may end up in future versions of our company's products.

BTW, to clarify the terminology confusion in your original statement of the question: Every Visio object -- the shapes on a page, the pages in a document, and even the document itself -- has a shapesheet. Think of the shapesheet as a spreadsheet full of values that control every aspect of the object from its appearance to its behavior. Under normal circumstances, you never see the shapesheet. But you can view it by selecting an object, then selecting Window/Show ShapeSheet from the Visio main menu. It is these windows that the sample code you posted will close.

A stencil on the other hand, contains masters, which you drag onto a page to create a drawing. A document may have zero, one or more stencils associated with it. Typically these stencil windows open when the document opens.

Author

Commented:
Oh wow Scott, I remember, shapesheet is for specifying properties (I haven't had to use one in years).   It's so easy to confuse this sheet with others in Visio--since "shapes" play a key role in Visio--I wish they'd named it a "property sheet" instead.

Thanks again for the help!

Leslie
User Experience Designer