Search code examples
vbams-wordword-2007

Can I change the behaviour of the Insert Table command in Word 2007?


In Word 2007, the "Insert" ribbon tab has a "Tables" group with a single drop-down button (labelled "Table").

The drop-down contains various menu items such as "Insert Table...", "Draw Table", etc. However, at the top of the drop-down there's a 10x8 grid that you can use to quickly choose the size of your new table using the mouse. (First question: what the heck is this thing called?!!).

I'd like to override the default behaviour, so that when the user "draws" a table using the widget mentioned above, I can change the paragraph style used within the table (and perhaps do some other clean-up).

I know how to override the "Insert Table..." command, but I can't figure out how to override the behaviour of the "widget". Can it be done?


Solution

  • Before you read on (because this will be long) I want to say that I did not solve this problem. However, in my attempts to solve -- and then work around -- this issue, I discovered quite a few things, which I am recording here in the hope that they will help someone else find the solution.

    While trying to determine what controls the 10x8 grid that one can use to insert a table, I discovered a macro listed within the Word commands named "TableInsertGeneral".

    TableInsertGeneral listed in Word Macros dialog box

    This macro, according to Suzanne S. Barnhill, will restore the grid if this feature ceases to function in earlier versions of Word (Insert Table Drop Down Missing). The macro, as it exists in Word 2007/2010, cannot be executed from the macro dialog box Run button. Double-clicking on the command dismisses the dialog box, but does not overtly do anything else. I additionally tried to intercept its function, by creating a VBA sub named TableInsertGeneral, but code I placed into this sub did not appear to be executed when I accessed the grid. However, based on my research I believe the TableInsertGeneral macro does have some connection to displaying the 10x8 grid.

    I also attempted to work around the problem by altering the Tables Gallery in the Word Ribbon. Because I could not directly access any code that controlled the grid, I attempted to hide the Tables Gallery and then replace it with a rebuilt gallery that excluded the grid function (thus allowing the paragraph style and other changes to work globally).

    First I downloaded some tools:

    Using the Custom UI Editor for Microsoft Office (which allows one to edit the customui.xml file inside a Word 2007 document or template without needing to create folder structures or maintain relationships between xml files) I opened a template and saved this code within the file:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui">
    <ribbon>
      <tabs>
        <tab idMso="TabInsert">
          <group idMso="GroupInsertTables" visible="false" />
          <group id="CustomGroupInsertTables" label="Tables" insertBeforeMso="GroupInsertIllustrations">
           <gallery id="CustomTableInsertGallery"
            label="Table"
            imageMso="TableInsertGallery" 
            size="large"
            columns="1" 
            rows="4"
            screentip="Table"
            supertip="Insert or draw a table into the document."
            keytip="T" 
            onAction="RibbonControl.galleryOnAction" >
            <item id="GridMessage" label="Draw Table Via Grid Has Been Removed" imageMso="TablesGallery" 
             supertip="Provides information on why this template has different Ribbon controls."/>  
            <button idMso="TableInsertDialogWord" />
            <button idMso="ConvertTextToTable" />
            <button idMso="TableExcelSpreadsheetInsert" />
           </gallery>
         </group>
        </tab>
      </tabs>
    </ribbon>
    </customUI>
    

    This successfully hid the original Tables gallery and replaced it with some of the functionality of the original Tables gallery. The 10x8 grid was gone, but I was unable to restore the Draw Table toggle button and Quick Tables gallery. As far as I can tell, the XML schema does not allow for embedding either of these (which exist in the out-of-the-box version of Word 2007) into an existing gallery. Because I do not like removing functionality (even for this partial solution, which I did not think would be employed), I added a button tied to a message box as the first item in the rebuilt gallery:

    Rebuilt Tables Gallery

    The code for connecting the new "Draw Table Via Grid Has Been Removed" button was placed in a module named RibbonControl:

    Sub GalleryOnAction(Control As IRibbonControl, selectedID As String, selectedIndex As Integer)
    
    If Documents.Count = 0 Then
     MsgBox "This control is disabled when there is no active document."
     Exit Sub
    End If
    
    Select Case Control.id
      Case "CustomTableInsertGallery"
        Select Case selectedIndex
          Case 0
            MsgBox "Explain changes to Ribbon interface here."
          Case Else
            'Do Nothing
        End Select 
    End Select
    End Sub
    

    I do not expect anyone to use this partial solution, however, if a means for restoring the two missing controls could be achieved, this might be a good workaround. By the way, I adapted most of this from Greg Maxey's Web site:

    Customizing the Ribbon

    If you read this far, thanks! And I wish you more success with your own attempts.