Quick Table Order, Macro, and Changes (Word 2007)

User avatar
Sundog
5StarLounger
Posts: 704
Joined: 28 Jan 2010, 22:47
Location: Alien Country (Roswell NM)

Quick Table Order, Macro, and Changes (Word 2007)

Post by Sundog »

In Word 2007, I have defined a new table and saved it in the Quick Tables gallery and BuildingBlocks.docxx. Naturally, Microsoft places my table at the bottom of all the other Microsoft stock tables, making me scroll down every time I want to insert it. It's shown in the General section, with name Std Table.

Q1) Is there a way to move my new table to the top of the Quick Tables list?

Q2) I tried to create a macro to insert my new table at the cursor, but it fails. The macro was created by recording my keyboard and mouse steps, not via VBA.
Try 1, resulting VBA:

Code: Select all

Sub InsertStdTable()
NormalTemplate.BuildingBlockEntries("Std Table").Insert Where:=Selection.Range, RichText:=True
End Sub 
Try 2:

Code: Select all

Sub InsertStdTable()
ActiveDocument.AttachedTemplate.BuildingBlockEntries("Std Table").Insert Where:=Selection.Range, RichText:=True
End Sub 
Error Message: "Error 5941, The requested member of the collection does not exist."

Any suggestions?

Q3) Is there a way to change some aspect of the saved Quick Table other than inserting it, fixing it in the Word document, and saving it to the Quick Tables gallery, then deleting the old one?
Sundog

User avatar
HansV
Administrator
Posts: 78531
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Quick Table Order, Macro, and Changes (Word 2007)

Post by HansV »

Q1) No.

Q2) Since you added the table to BuildingBlocks.dotx and not to Normal.dotm, it is not part of the building blocks of NormalTemplate or AttachedTemplate. You have to insert it from BuildingBlocks.dotx. It is not very attractive to hard-code the path to this file in your macro, so you can loop through all templates to find it, then loop through the building block entries:

Code: Select all

Sub InsertStdTable()
    Dim tpl As Template
    Dim i As Long
    For Each tpl In Templates
        If tpl.Name = "Building Blocks.dotx" Then
            For i = 1 To tpl.BuildingBlockEntries.Count
                If tpl.BuildingBlockEntries(i).Name = "Std Table" Then
                    tpl.BuildingBlockEntries(i).Insert _
                        Where:=Selection.Range, RichText:=True
                    Exit Sub
                End If
            Next i
        End If
    Next tpl
End Sub
Q3) If you're good at programming XML, you could perhaps manipulate the building block in the .dotx file directly, otherwise the method you describe is the only one, as far as I know.
Best wishes,
Hans

User avatar
Sundog
5StarLounger
Posts: 704
Joined: 28 Jan 2010, 22:47
Location: Alien Country (Roswell NM)

Re: Quick Table Order, Macro, and Changes (Word 2007)

Post by Sundog »

HansV wrote:Q2) Since you added the table to BuildingBlocks.dotx and not to Normal.dotm
Ah so! Of course, Microsoft's default when saving a Quick Table, on my system anyway, is BuildingBlocks.dotx. If I repeat my steps but choose Normal.dotm at the Save, this is the resulting code, which works perfectly.

Code: Select all

Application.Templates( _
        "C:\Users\Dale\AppData\Roaming\Microsoft\Templates\Normal.dotm"). _
        BuildingBlockEntries("Std Table").Insert Where:=Selection.Range, RichText _
        :=True
It still shows up in the code as a "BuildingBlockEntries", but it works.

Thanks, Hans.
Sundog

User avatar
HansV
Administrator
Posts: 78531
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Quick Table Order, Macro, and Changes (Word 2007)

Post by HansV »

But in that case, you can use NormalTemplate instead of Application.Templates("C:\Users\Dale\AppData\Roaming\Microsoft\Templates\Normal.dotm"). This is both shorter and independent of the path to Normal.dotm.
Best wishes,
Hans

User avatar
Sundog
5StarLounger
Posts: 704
Joined: 28 Jan 2010, 22:47
Location: Alien Country (Roswell NM)

Re: Quick Table Order, Macro, and Changes (Word 2007)

Post by Sundog »

Veeerrrryyyy Good! Code now looks like this:

Code: Select all

NormalTemplate.BuildingBlockEntries("aaaaStd Table").Insert Where:=Selection.Range,RichText:=True
And the way to get it to the top of the Quick Tables gallery is to choose "Built In" instead of "General" and give its name the Yellow Pages treatment.
Sundog

User avatar
HansV
Administrator
Posts: 78531
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Quick Table Order, Macro, and Changes (Word 2007)

Post by HansV »

Thanks for sharing that.
Best wishes,
Hans