Insert Building Blocks by VBA

User avatar
Sam1085
3StarLounger
Posts: 318
Joined: 23 Aug 2016, 07:43
Location: Sri Lanka

Insert Building Blocks by VBA

Post by Sam1085 »

Hi,

I've created a pre formatted text box and added it into my macro file (.dotm) as a Building Block (Insert >> Quick Parts...)

I've to insert the text box while running my macro code.
Let's say basic macro process:
01. Cut selected text
02. Set left and right margin into 72pt
03. Insert Text box (By running the following code)

Code: Select all

    Application.Templates( _
        "C:\Program Files (x86)\Microsoft Office\Office14\STARTUP\MyMacros.dotm" _
        ).BuildingBlockEntries("MyTextBox1").Insert Where:= _
        Selection.Range, RichText:=True
04. Select the text box frame
05. Paste Text

Macro works as I needed. Final result:
img1.png
My question is, this macro is only works for my system (MS Office 2010 32bit). Reason is the building block call reference as the following path:
C:\Program Files (x86)\Microsoft Office\Office14\STARTUP\

Is there any alternative way to compatible this macro to any MS word version?

Many Thanks!
You do not have the required permissions to view the files attached to this post.
-Sampath-

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Insert Building Blocks by VBA

Post by Rudi »

Below is code from Greg Maxey (taken from this thread) that I slightly modified for your purpose...

Note that the quick part you created should be stored in a specific location (based on the parameters in the code). (See the image for the parameters).
This can be modified if needed. If this quick part is placed in the same location, this should operate for any Word version supporting building blocks.
Image 007.jpg
Note: Code assumes the building block is named "Test" and saved in the template QuickPart gallery, General category.

Code: Select all

Sub ScratchMacro0()
'A basic Word macro coded by Greg Maxey

'Note:
'Code assumes the building block is named "Test" and saved
'in the template QuickPart gallery, General category.

Dim oTmp As Template
Dim oBB As BuildingBlock
  Templates.LoadBuildingBlocks
  For Each oTmp In Templates
    If UCase(oTmp.Name) = "NORMAL.DOTM" Then Exit For
  Next oTmp
  If Not oTmp Is Nothing Then
    On Error Resume Next
    Set oBB = oTmp.BuildingBlockTypes(wdTypeQuickParts).Categories("General").BuildingBlocks("Test")
    If Err.Number = 0 Then
      oBB.Insert Selection.Range, True
    Else
      MsgBox "The BB that you specifically hardcoded in your macro which would lead one to assume that is exists does not exist.", vbExclamation
    End If
  Else
    MsgBox "The required building block template is not loaded.", vbExclamation
  End If
lbl_Exit:
  Exit Sub
End Sub
You do not have the required permissions to view the files attached to this post.
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
Sam1085
3StarLounger
Posts: 318
Joined: 23 Aug 2016, 07:43
Location: Sri Lanka

Re: Insert Building Blocks by VBA

Post by Sam1085 »

Thanks Rudi,

As you said yes, quick parts should be stored in a specific location. Therefore I changed my mind and add a popup msg box when it's not installed in the startup folder.

The above mentioned thread is very helpful for me. Thanks again.
-Sampath-