Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-07-2022, 03:29 AM
LSLSLS LSLSLS is offline error handler: inserting building blocks when multiple cells in a table are selected creates error Windows 10 error handler: inserting building blocks when multiple cells in a table are selected creates error Office 2016
Novice
error handler: inserting building blocks when multiple cells in a table are selected creates error
 
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, 10 views)
Reply With Quote
  #2  
Old 01-07-2022, 05:18 AM
gmayor's Avatar
gmayor gmayor is offline error handler: inserting building blocks when multiple cells in a table are selected creates error Windows 10 error handler: inserting building blocks when multiple cells in a table are selected creates error Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,105
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #3  
Old 01-07-2022, 05:46 AM
LSLSLS LSLSLS is offline error handler: inserting building blocks when multiple cells in a table are selected creates error Windows 10 error handler: inserting building blocks when multiple cells in a table are selected creates error Office 2016
Novice
error handler: inserting building blocks when multiple cells in a table are selected creates error
 
Join Date: Jan 2022
Posts: 2
LSLSLS is on a distinguished road
Default

Works perfectly and solves the problem. Thanks for your help!
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
error handler: inserting building blocks when multiple cells in a table are selected creates error 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
error handler: inserting building blocks when multiple cells in a table are selected creates error Open Quickpart file normalemail.dotm - edit multiple building blocks rollno Word 7 02-24-2014 04:29 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 05:01 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft