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

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)
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 
            intCounter = Windows.Count 
            intCounter = intCounter - 1 
        End If  
End Sub

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott HelmersVisio Consultant, Trainer, Author, and DeveloperCommented:
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"
leslieinvaAuthor 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.

Scott HelmersVisio Consultant, Trainer, Author, and DeveloperCommented:
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.

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Scott HelmersVisio Consultant, Trainer, Author, and DeveloperCommented:
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:


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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
leslieinvaAuthor Commented:
You ROCK Scott!  That's the right code  Thanks for the explanation too.
Scott HelmersVisio Consultant, Trainer, Author, and DeveloperCommented:
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.
leslieinvaAuthor 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!

User Experience Designer
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Visio

From novice to tech pro — start learning today.