Consulting

Results 1 to 3 of 3

Thread: Selecting a Text Box Building Block after Insertion

  1. #1
    VBAX Regular
    Joined
    Mar 2018
    Location
    Leesburg
    Posts
    68
    Location

    Selecting a Text Box Building Block after Insertion

    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

  2. #2
    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
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Regular
    Joined
    Mar 2018
    Location
    Leesburg
    Posts
    68
    Location
    Graham, thank you so much! This did the trick.
    Thanks also for the amazingly quick reply.
    Regards,
    Doug

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •