PDA

View Full Version : [SOLVED:] Selecting a Text Box Building Block after Insertion



dbowlds
05-10-2018, 04:07 AM
Hello, I have a text box building block in a template in my STARTUP folder. I am able to insert it into the document via VBA. But, once inserted, I need to be able to select it and modify things like the fill color. However, I am unable to figure out how to select it once it has been inserted.
The building block is a text box that is wrapped tight with the text. The property of the building block is set to "Insert Contents Only." So, once inserted, the text box is anchored to the paragraph in which the cursor resides at the time of insertion. The text box is automatically positioned to the right of the paragraph in which the cursor resides at the time of insertion. Insertion is accomplished via the text context menu XML callback to a VBA module.
The text box is considered to be a shape, right? Is there a way to find and select the shape that is attached to the current paragraph immediately after insertion?
I am using Word 2016.
Code follows. I modified a few things to make it public (indicated by use of brackets).
Any help would be most appreciated.
Thanks for your consideration.
Doug



'Callback for ctxbtnTextBox onActionSub ctxInsertTextBox_click(control As IRibbonControl)
Dim MyTemplate As Template
Dim MyBB As BuildingBlock
Dim myPath As String
myPath = Application.StartupPath & "\[this is my template].dotm"
'Set the template to the one that holds the building block
Set MyTemplate = Templates(myPath)

'Access the building block through the type and category
Set MyBB = MyTemplate.BuildingBlockTypes(wdTypeQuickParts) _
.Categories("[my category]").BuildingBlocks("[My Text Box]")

'Insert the BB
MyBB.Insert Selection.range

'Select the BB just inserted
'TBD
End Sub

gmayor
05-10-2018, 05:59 AM
Maybe

Dim MyTemplate As Template
Dim myBB As Range
Dim oShape As Shape
Dim myPath As String
Dim oRng As Range
myPath = Application.StartupPath & "\[this is my template].dotm"
'Set the template to the one that holds the building block
Set MyTemplate = Templates(myPath)
With ActiveDocument
Set myBB = MyTemplate.BuildingBlockEntries("TextBox").Insert _
(Where:=Selection.Range)
Set oShape = myBB.ShapeRange(1)
With oShape
Set oRng = .TextFrame.TextRange
oRng.Text = "This is the text"
oRng.Font.ColorIndex = wdRed
oRng.Font.Size = 14
End With
End With

dbowlds
05-10-2018, 07:31 AM
Graham, thank you so much! This did the trick.
Thanks also for the amazingly quick reply.
Regards,
Doug