Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I hide a specific toolbar in Microsoft Word?

-- AG

SpacerHey, Scripting Guy! AnswerScript Center

Hey, AG. Now this question brings back memories. When our editor joined the team one of her first actions was to make us redo a whole bunch of screenshots we had made for the Scripting Week 2 webcasts. Why? Because she didn’t think we should include screenshots that showed a custom Microsoft Word toolbar we use when working with content for the Script Center. Thus we had to open Word, hide the toolbar, and redo all the screenshots. If we’d had a script like the one you’re looking for, we wouldn’t have had to manually open Word and hide the toolbar. Granted that wouldn’t have been a huge time savings, but, still….

Note. We kid our editor from time-to-time, but, really, we have no complaints about her whatsoever. Well, OK, it would be nice if she’d only use one parking space when parking her broomstick each morning, but other than that….

But let’s get down to business. You want a script that can hide a specific toolbar in Microsoft Word, right? Well, how about this simple little script, which hides the Formatting toolbar:

Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objTool = objWord.CommandBars("Formatting")
objTool.Visible = False

As you can see, it’s a simple task - hide a toolbar - and an equally simple script. We begin by creating an instance of the Word.Application object and then setting the Visible property to True (just so we can see what’s going on). We then use this line of code to create an object reference to the Formatting toolbar, which is part of the Word CommandBars collection:

Set objTool = objWord.CommandBars("Formatting")

To hide the toolbar we simply set the toolbar’s Visible property to False. If we wanted to show the toolbar all we’d have to do is set the Visible property to True.

No doubt one question that immediately pops into mind is this: how did we know this particular toolbar has the name Formatting? Well, if you click the View menu in Word and then click Toolbars you’ll see a list of the toolbars available. The names shown in the menu are the same names you use in your scripts.

Note. Couldn’t we just enumerate all the items in the CommandBars collection to retrieve a list of toolbar names? Yes, we could. Unfortunately, though, the CommandBars collection also includes menus, shortcut menus, and submenus, and there doesn’t appear to be a way to programmatically separate toolbars from these other items. Therefore your best bet is just to open Word and take a look at the toolbar names.

The script we showed you actually opens up Word, hides the toolbar, and then leaves Word open. If you’d rather just configure the toolbar setting without seeing anything on screen you can use this script, which starts up Word in a hidden window, hides the toolbar, and then exits the program. You won’t see anything happen on screen, but the next time you start Word the Formatting toolbar will be hidden:

Set objWord = CreateObject("Word.Application")
Set objTool = objWord.CommandBars("Formatting")
objTool.Visible = False
objWord.Quit

One final point we should make is that - as we’ve seen - it’s possible to hide a toolbar in Word. However, the toolbar will still be available: it just won’t be visible at the moment. For better or worse (probably for better) you can’t delete any of Word’s built-in toolbars; the best you can do is hide them from view.

Anyway, thanks for your question AG. And now we have a question for you. We’d like to get our editor a small token of our appreciation, but we have no idea where to find eye of newt in the Seattle area. Any ideas?

Editor’s Note. The preceding was a work of fiction (with the exception of the scripting material). Any resemblance to editors past or present is purely coincidental. Although there is no resemblance to editors present. Past, well….