PDA

View Full Version : [SOLVED:] Insert Building Block Table



dbowlds
07-25-2018, 03:30 AM
Hello, I have a need for a macro that inserts a simple, 2 column, 1 row table at the cursor location in a document. The table would have borders set to none, and the two cells would be formatted as a specific custom bullet style.
After lots of search the Internet for clues, I could not figure out how to insert and format such a simple table, so instead I decided it might be easiest to create the table I want as a building block (BB) and just have the macro insert it. I created and saved such a BB in my master template (the template that contains all my code). The BB is saved as the type: "Insert content in its own paragraph." Doing it this way ensures the table doesn't get inserted in the middle of a sentence.
However, if I run the code below, for some odd reason the building block table gets added to my document but with borders. Is there some way for the code to then immediately select the table and set the outside borders to none?
Any help would be most appreciated.


Sub rxbtnInsertBulletTable_click(control As IRibbonControl)
Dim MyTemplate As Template
Dim myBB As range
Dim myPath As String
myPath = Application.StartupPath & "\[template name]"
'Set the template to the one that holds the building block
Set MyTemplate = Templates(myPath)
With ActiveDocument
Set myBB = MyTemplate.BuildingBlockEntries("BulletTable").Insert _
(Where:=Selection.range)
'Select table just inserted and format the borders.style to none
End With
End Sub

gmayor
07-25-2018, 04:20 AM
The following additions should work.

Dim oTable as Table
'............
Set oTable = myBB.Tables(1)
For Each oBorder In oTable.Borders
oBorder.LineStyle = wdLineStyleNone
Next oBorder

dbowlds
07-25-2018, 05:26 AM
Graham, you are amazing!
It worked perfectly.
:clap:THANK YOU!!!!!!

gmaxey
07-25-2018, 07:25 AM
If you create a building block table without borders then a building block should be inserted without borders. It does here. You might try:

Set myBB = MyTemplate.BuildingBlockEntries("BulletTable").Insert(Where:=Selection.Range, RichText:=True)

dbowlds
07-25-2018, 09:50 AM
Hi Greg. I agree, if I create a BB table without borders it should show up without borders when inserted. It does the very first time you insert one. But then any time thereafter, including when you close the document and open it, or create a new document off the template, it always gets inserted with borders. Very strange.
I tried your fix above (adding the RichText:=True statement) but that didn't work - it still shows up with borders. When I look at the actual BB via the BB organizer, it shows no borders.
The code works using Graham's method above, however. I know it is adding an extra processing step, and the other method should work, but I think I am happy with it since it works. I will chalk this up to one of those MSFT Word oddities. (Or, perhaps it is operator head-space on my part!)
Thank you for your assistance.
Doug