|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
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 |
#2
|
||||
|
||||
It is easy enough to determine whether the selection is in a table and if it is to select one of the cells in that table and set the range to that cell's content. e.g.
Code:
Sub Macro1() Dim WrdTemplate As Template Dim objBB As BuildingBlock Dim WrdRng As Range Set WrdRng = Selection.Range If WrdRng.Information(wdWithInTable) = True Then 'the selection is in a table Set WrdRng = Selection.Cells(1).Range 'the first cell in the selection WrdRng.End = WrdRng.End - 1 'omit the end of cell character from the range End If Set WrdTemplate = Application.Templates("filepath") Set objBB = WrdTemplate.BuildingBlockTypes(wdTypeCustom1).Categories("Legislation").BuildingBlocks("END") objBB.Insert WrdRng Set objBB = Nothing Set WrdRng = Nothing Set WrdTemplate = Nothing End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
|||
|
|||
Works perfectly and solves the problem. Thanks for your help!
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Multiple entries in dropdown lists with building blocks | chriscla | Word VBA | 16 | 04-04-2020 10:41 PM |
Error Handler If User Does Not Want to Overwrite File | ChrisOK | Excel Programming | 4 | 04-03-2020 08:31 PM |
Error when inserting Quick Table created | taryn | Word | 2 | 09-11-2017 05:44 AM |
Formatting Issues When Using If Statements and Inserting Building Blocks/Autotext | sanko787 | Mail Merge | 1 | 07-05-2014 09:00 PM |
Open Quickpart file normalemail.dotm - edit multiple building blocks | rollno | Word | 7 | 02-24-2014 04:29 PM |