PDA

View Full Version : [SOLVED:] Adding a table Building Block with a Command Button



mtnbiker1185
02-16-2017, 01:03 PM
I am building a form in Word 2016 and have a table that I am using for users to input some information. I saved the table as a building block under Quick Tables and would like to make it so a user can add another item table if needed when filling out the form. I am new to VBA so I have no clue as to where to start with regards to the code. I have the button added but that is as far as I made it. I attached a screenshot to give better clarification. I would like the new table to show up below the current table and above the Add Item button.

If it helps the building block is named 'New Item' and the button is named NewItem. I also have added a Bookmark called New_Item under the current table. I will be locking down the form so the user can only fill out the form and not make changes to anything....if that matters. 18393

Thanks!

mtnbiker1185
02-16-2017, 01:40 PM
I was able to get it to work by recording a macro and then assigning the macro to the button; however, when I click the button and the table appears, the button disappears. Also, when I lock the form down so only the form fields can be filled out the macro fails. Any advice on how to get the button to just move down as new item tables are added and how to get the macro to run when the form is locked down?

gmaxey
02-16-2017, 03:21 PM
Include your bookmark as part of the building block. This way the new table and new bookmark will replace the old bookmark.



Private Sub CommandButton1_Click()
Dim oRng As Word.Range
Dim oILS As InlineShape
Set oRng = ActiveDocument.Bookmarks("bmTarget").Range
Application.Templates(ThisDocument.AttachedTemplate). _
BuildingBlockEntries("FormTable").Insert Where:=oRng, RichText:=True
Set oILS = Selection.InlineShapes(1)
oILS.Range.Paragraphs(1).Previous.Range.Delete
End Sub

gmaxey
02-16-2017, 03:44 PM
The part beginning with Set oILS ...

prevents empty paragraphs from piling up between your bookmark and command button.

mtnbiker1185
02-16-2017, 04:05 PM
That works great, thank you!