0
votes

I'm trying to add a custom building block at the click of a button in MS word 10. Below is the code currently attached to my activeX button.

Private Sub CommandButton1_Click()
   Dim objTemplate As Template
   Dim objBB As BuildingBlock

' Set the template to store the building block
  Set objTemplate = ActiveDocument.AttachedTemplate

' Access the building block through the type and category
  Set objBB = objTemplate.BuildingBlockTypes(wdTypeCustom5) _
 .Categories("General").BuildingBlocks("Experience")

 ' Insert the building block into the document replacing any selected text.
   objBB.Insert Selection.Range
 End Sub

My problem is, as this code is invoked at the click of a button, the button becomes the "Selection.Range" and is thus replaced. I looked all around for alternate codes that mention of different "where" specification and found nothing.

I only found two links(can't find the urls in my history rightnow, will update shortly)

  1. It mentioned "Paragraphs(1)" instead of "Selection.Range", but this is an absolute location while I would need something relative (Before the button)

  2. Using InsertBefore method which I suppose applies only to text (it was used to insert text in the example) as when I tried it for building blocks it didnt work

P.S I'm relatively new to VBA

2

2 Answers

0
votes

Ok, I solved the problem with the following code, posting it for others who might drop by in the future.

Private Sub CommandButton1_Click()
   Dim objTemplate As Template
   Dim objBB As BuildingBlock

   ' Set the template to store the building block
   Set objTemplate = ActiveDocument.AttachedTemplate

   ' Access the building block through the type and category
   Set objBB = objTemplate.BuildingBlockTypes(wdTypeCustom5) _
  .Categories("General").BuildingBlocks("Experience")

  ' Insert the building block into the document replacing any selected text.
   Selection.MoveUp Unit:=wdLine, Count:=1
   objBB.Insert Selection.Range
  End Sub

Basically just added the following lines just before inserting the BuildingBlock

       Selection.MoveUp Unit:=wdLine, Count:=1

Thanks everyone for the assistance.

0
votes

When in "Creation mode", goto the "properties" toolbox of your CommandButton1, change the property TakeFocusOnClick from True to False:

enter image description here

The focus will stay onto the paragraph you selected.

If you want it just after your CommandButton you can add the following at the end of your sub:

    ...
    CommandButton1.Select
    Selection.HomeKey wdLine
    objBB.Insert Selection.Range
End Sub