View Single Post
 
Old 01-07-2022, 03:29 AM
LSLSLS LSLSLS is offline Windows 10 Office 2016
Novice
 
Join Date: Jan 2022
Posts: 2
LSLSLS is on a distinguished road
Default error handler: inserting building blocks when multiple cells in a table are selected creates error

Hello and Happy New Year.

First of all, I'm new to this (as I'm sure you can see), but trying to learn as best I can, by doing. I have hit a wall, however.

I have a macro which inserts a building block from my custom library when a button is clicked in the ribbon in word (button added using Custom UI Editor).

This works fine, though if a user has mistakenly selected a number of cells in a Table in the word document and tries to insert the building block, it creates an error (see figure attached).

My approach set out below.

Sub Macro1()

Dim WrdTemplate As Template
Dim objBB As BuildingBlock
Dim WrdRng As Range

Set WrdTemplate = Application.Templates("filepath")

Set objBB = WrdTemplate.BuildingBlockTypes(wdTypeCustom1).Cate gories("Legislation").BuildingBlocks("END")

objBB.Insert Selection.Range
End Sub



I tried to get around this by changing

objBB.Insert Selection.Range

to

objBB.Insert ActiveDocument.Range

and this does avoid the error, though it overwrites the last building block I added and I need to be able to add multiple.

Is there a way to handle this error so that, if a user has selected multiple cells in a table and they try to insert a building block there, an error box comes up and prevents the action?

Many thanks
Attached Images
File Type: png error.png (3.7 KB, 12 views)
Reply With Quote