• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5933
  • Last Modified:

How to programmatically disable Word 2010 Ribbon Tab and Commands

I developed a VB.Net 2008 application that automates Microsoft Word 2003. We are migrating to Word 2010 and I need to know how to control what commands are available to the operator.

The application either hides a toolbar or disables a command to prevent the operator from using it. For example, the application hides the Standard toolbar which contains the Save button we don't want the operator to use.  A custom macro was written to save the document and perform necessary updates to the database. I also don't want the operator to use Word's X button to close a document so I programmatically disable it when the document is opened. Another macro was written to close the document and perform updates to the database.

It is imperative the operator use the custom commands written specifically for this application to Save and Close a Word documents. These custom commands are defined in the document's Word template and do appear in the Add-Ins Ribbon Tab in Word 2010.

I would like know if there is a way to programmatically hide or disable the File Ribbon Tab or the commands within the Tab and how to disable the Close(X) button when the application opens a document in Word 2010. The program code to do this for Word 2003 doesn't work in Word 2010.

I would also like to know if there is a way to show the custom commands, i.e. macros, that appear in the Add-Ins tab could be placed in a separate toolbar under the Ribbon. This toolbar would behave the same way as the Quick Access Toolbar; always visible regardless of which Ribbon Tab is selected.

Thank you very much in advance,  Curt
  • 8
  • 7
1 Solution
You can disable ribbon items using the RibbonXML file, I think the problem with that approach there are so many ways to do the same thing..
users can use the Keyboard,  Macros or add custom buttons to the ribboon/quick access tolbar

I have an app with similar requirements and I found using the before close and before save events more reliable. Locking down office applications is difficult because there are so many ways to accomplish the same thing.

The other advantage of trapping the events is that the user can continue using word the same as always.

CurtHugoAuthor Commented:
JonMny, sorry for the delay in getting back to you.

I had considered using events early in the development of this application and think it would be an excellent alternative to the way I had written the application. However there is one application that opens, modifies, saves, and closes word documents without human intervention in which I would not want the code behind the Before Save and Before Close events to run. This is the final letter printing application which prints from 1 to many letters at a time upon operator request.

I am hoping to find a solution that will minimize the changes to the applications already in production. I think changing to the event driven approach you suggested will require considerably more changes to the applications than we would prefer to make at this time.

However, I am now going to rethink using the On Save/On Close events to manage these processes to see if this would be a viable solution to our migration to Word 2010.

You did mention using the RibbonXML file which I am not familiar with. I will look into that as well to see how ribbon items can be manipulated with this tool.

I would be interested to here about the application you wrote with similar requirements. The app I developed is used to manage the correspondence our company employees create and the printing of these letters.    

Please let me know if you have anything else to add at this time.
The application we used required us to override the default save function to create a backup and also serialized data in documents to xml.  We also created an excel application in that one we did remove some of the copy/paste buttons/menus. This also requred a keyboard trap so that the ctrl-c and ctrl-v didnt work. we also changed the print function and replaced it with our own custom options.

the printing and saving worked with the events. Copy/paste was the one that caused issues because users always found a way to work around the disabled buttons until we added an event trap on sheet/change.

For your production applications it makes sense to minimize the changes

Here are some links that should be helpful.


Clear all ribbons

Keyboard hooks

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

CurtHugoAuthor Commented:
It does sound like what you did in your application is what I have done in mine but in different ways.

The first 2 links looked interesting. I just don't have the understanding yet how to put it to use. For example, I'm not sure exactly what happens when you Invalidate a buit-in control. Does that disable the control, e.g. the File Tab? If yes, then that would be what I'd want to invalidate the controls from the VB.Net application that opens a Word document.

One other thing I had asked that you did not address. I would also like to know if there is a way to show the custom commands, i.e. macros, that appear in the Add-Ins tab could be placed in a separate toolbar under the Ribbon. This toolbar would behave the same way as the Quick Access Toolbar; always visible regardless of which Ribbon Tab is selected.

We are getting a book this week, Mastering VBA for Office 2010, I'm hoping will have what I need to better understand how to put all of this to use to accomplish what I want to do.


the easy way to hide a tab would be adding this to your custom xml file.

<tab idMso="TabHome" visible="false" />

(I don't have 2010 so I can't test the file menu but should be the same)

If you don't need to toggle the visibility of these items this is what I would recommend. In the XML file you can also specify callbacks and events on each button if  you want to change the default behavior

the invalidate method will cause the ribbon to repaint and call any callbacks. This would allow you to set the visibility.

I am not sure about adding a custom toolbar under the ribbon.

you can place your custom commands anywhere, for example if you use the ribbon designer in visual studio it has a position property that allows you to specify before/after a built in tab.

or in the xml file
<Tab ... insertAfterMso="msoID" ...>

CurtHugoAuthor Commented:
I again appologize for the delay in getting back to you. I did find a very helpful chapter in Mastering VBA in Office 2010 that dealt with programming the Office 2010 Ribbon. The chapter focused on using XML to modify the ribbon.  What I learned was that when you add the XML to a custom template, only the Word documents using that template are affected. I also learned in this chapter how to create a new tab containing buttons to callback to my macros via the onAction command. I then discovered you can add a control or an entire group in a tab to the Quick Access Toolbar. Once again it only affects the documents that use this template.  This was one of the most important things I wanted to do.

I then found this article about repurposing a Built-In control which I have really found very useful for what I wanted to do.

From this article I learned how to repurose the FileSave and FileExit controls which is a great feature. However I have not been able to find the commands to repurpose the FilePrint and the X button to close the document. It seems odd to me that you can repurpose the Close and Exit commands from the File tab but not the Print command or the X button. If there was a way to repurpose or disable the X button, that would be good enough to complete the requirements for this application.

I think by making the commands to call the cusom macros available in the QAT as well as in a Tab, it will make it easy for the user to get to the commands we want them to use from either place depending on their preference.

I assume you already know how to do most if not all the things I just mentioned.

Please let me know what you think and/or if you have any idea how to repurpose the X button and the File Print command. Thanks, Curt


For the print doenst using this work?

<command idMso="Print" onAction="myPrint"/>

Not sure about the x button since that's more of a window function.

Again I think the events would help with this. You can capture the before close event cancel it and then run your code

CurtHugoAuthor Commented:
I was just looking into the events and found there is not a before close or before save event. Just Document Close. What am I missing?
Not sure, if you go to the document in design mode and show properties and click on the events button you will see all of the available events.

CurtHugoAuthor Commented:
I don't see the Events button in the Properties window. I have MS Office Standard 2010 installed on my computer. Should I be using the Professional version so that I can see the Events button?

The only way I know how to get into Design mode is from View / Macros. Is there another way?
Sorry, I thought you were using visual studio. If you want to do it from word you would need to use a class module to trap the event. To do this, insert a class module in your doc's VBA project.

Public WithEvents appWord As Word.Application

Private Sub appWord_DocumentBeforeClose(ByVal Doc As Document, Cancel As Boolean)

' Code you want in the close event
'cancel= true to prevent close

End Sub

In the ThisDocument module, put this code:

Dim X As New clsWordEventTrapper
Private Sub Document_Open()

Set X = Word.Application

End Sub

note in my example you would name the class clsWordEventTrapper

CurtHugoAuthor Commented:
I was finally able to get working all of the must-have functionality for this application, almost all from XML in a custom template and I didn't have to use Document Events. I have either repurposed commands like File Save, FilePrint to callback the corresponding custom macro in the document template file or I disabled commands like File New, File Open, File Save As and even the Options button on the File tab that will prevent user from customizing the Ribbon. It really is a very nice solution.

I even figured out why the X button for Word was not being disabled when the VB.Net program opened the document and ran this code to disable the X button.

            m_inthWnd = FindWindow(vbNullString, strWordCaption)
            m_inthMenu = GetSystemMenu(m_inthWnd, 0)
            m_intwFlags = MF_BYCOMMAND Or MF_GRAYED
            m_intwResult = EnableMenuItem(m_inthMenu, SC_CLOSE, m_intwFlags)

My application was written to open a .doc file and when opened, Word 2010 adds [Compatibility Mode] to the caption. I did not account for that in the FindWindow function and therefore it could not find the window. When a .docx file is opened, the X button is disabled.  

Here is the XML markup to either repurpose or disable built-in commands I thought you might want to look at. However, the Print command and New command in the File tab does not repurpose or get disabled.  
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="onLoad" >
      <command idMso="FileSave" onAction="modInterfaceNLW.FileSaveRepurpose" />
      <command idMso="FileExit" onAction="modInterfaceNLW.FileExitRepurpose" />      
      <command idMso="FileNew" enabled="false" />      
      <command idMso="FileNewDefault" enabled="false" />      
      <command idMso="FileNewBlankDocument" onAction="modInterfaceNLW.FileNewRepurpose" />      
      <command idMso="FileClose" onAction="modInterfaceNLW.FileExitRepurpose" />      
      <command idMso="FileCloseAll" onAction="modInterfaceNLW.FileExitRepurpose" />      
      <command idMso="FileCloseOrCloseAll" onAction="modInterfaceNLW.FileExitRepurpose" />      
      <command idMso="FileCloseOrExit" onAction="modInterfaceNLW.FileExitRepurpose" />      
      <command idMso="PrintPreviewAndPrint" enabled="false" />
      <command idMso="FilePrintQuick" onAction="modInterfaceNLW.FilePrintRepurpose" />
      <command idMso="ApplicationOptionsDialog" enabled="false" />
      <command idMso="FileOpen" enabled="false" />
      <command idMso="FileOpenRecentFile" enabled="false" />
      <command idMso="FileSaveAs" enabled="false" />
  <ribbon startFromScratch="false">

I am planning to convert all of the .doc files to .docx programmatically and changing the attached template to .dotm. This something like what I have done before. I just need to tweek the code to do this.

Anyway, I thank you for your suggestions and will award you the points. If would like to contact me for anything, my email address is Curt.Hugo@Yahoo.com.

I'll Accept the solution after I hear back from you with any comments you might have.
yes that's the issue when usng the findwindow command, hard to know exactly what the window caption will be. Excel has the application.hwnd property but for some reason word doesn't.
you could use  FindWindow(L"OpusApp", NULL);

more details

Glad you got everything working,  good to know so the xml markup works for most everythng.   I will have to test see if it works as well in 2007

CurtHugoAuthor Commented:
JonMny was the only one to respond to my question and provided some good information which helped me find solutions to the issues I was having.

I almost always get answers to my questions/issues from Experts Exchange contributors.

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 8
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now