Log in

View Full Version : Using a checkbox to insert building blocks at bookmarks



Amich
11-25-2019, 05:05 AM
I have a template that opens with a Userform from which the user can select up to 11 options (checkboxes) to enable blocks of text (BuildingBlocks) to be included in the final document at a bookmark. So far I have been unable to get his to work successfully for even one of the checkboxes, and am now trying the below code that I adapted from another post on this forum (with thanks to Graham Mayor). I am getting the Compile error - User-defined type not defined with the code below at line Sub AutoTextToBM . Can anybody help with this please, I have also tried using the template name instead of ActiveDocument.AttachedTemplate but that just throws up other errors?

Option Explicit



Private Sub CommandButton1_Click()
If CheckBox1.Value Then
AutoTextToBM "bmPlace", "ActiveDocument.AttachedTemplate", "BB_PWC"
End If
End Sub


Sub AutoTextToBM(bmPlace As String, oTemplate As ActiveDocument.AttachedTemplate, BB_PWC As String)
'Graham Mayor - - Last updated - 17 Jul 2018
'strBMName is the name of the bookmark to fill
'oTemplate is the template with the autotext - probably ActiveDocument.AttachedTemplate
'strAutotext is the name of the autotext entry
Dim oRng As Range
On Error GoTo lbl_Exit
With ActiveDocument
Set oRng = .Bookmarks(bmPlace).Range
Set oRng = oTemplate.AutoTextEntries(BB_PWC).Insert _
(Where:=oRng, RichText:=True)
.Bookmarks.Add Name:=bmPlace, Range:=oRng
End With
lbl_Exit:
Exit Sub
End Sub

gmayor
11-25-2019, 05:51 AM
Your problem is undoubtedly that you have changed my code. Change it back to what is should be i.e. as below. The Button_Click part of the code assumes that the bookmark is "bmPlace", the Template is ActiveDocument.AttachedTemplate (without the quotes) and the autotext entry in that template is "BB_PWC". The Button_Click macro delivers the appropriate values to the main macro.



Option Explicit
Private Sub CommandButton1_Click()
If CheckBox1.value = True Then
AutoTextToBM "bmPlace", ActiveDocument.AttachedTemplate, "BB_PWC"
End If
End Sub

Sub AutoTextToBM(strbmName As String, oTemplate As Template, strAutotext As String)
'Graham Mayor - - Last updated - 17 Jul 2018
'strBMName is the name of the bookmark to fill
'oTemplate is the template with the autotext - probably ActiveDocument.AttachedTemplate
'strAutotext is the name of the autotext entry
Dim oRng As Range
On Error GoTo lbl_Exit
With ActiveDocument
Set oRng = .Bookmarks(strbmName).Range
Set oRng = oTemplate.AutoTextEntries(strAutotext).Insert _
(Where:=oRng, RichText:=True)
.Bookmarks.Add Name:=strbmName, Range:=oRng
End With
lbl_Exit:
Exit Sub
End Sub

Amich
11-25-2019, 06:18 AM
Thank you Graham, this now works perfectly, for one checkbox anyway!

gmayor
11-25-2019, 10:19 PM
You can use the same function to populate as many bookmarks as you have with suitable autotexts e.g.


Private Sub CommandButton1_Click()
If CheckBox1.value = True Then
AutoTextToBM "bmPlace", ActiveDocument.AttachedTemplate, "BB_PWC"
End If
If CheckBox2.value = True Then
AutoTextToBM "Name of Bookmark", ActiveDocument.AttachedTemplate, "Name of Autotext"
End If
End Sub

Amich
11-28-2019, 03:14 AM
Hi Graham,

I'm just looking at this again now and trying to find a way to make multiple building blocks, selected by the 11no. checkboxes, append consecutively to the bm.

At the moment, with the code below, only the last building block selected via the userform checkbox appears in the document. Is there a way to include multiple selections within one bm?

Sub AutoTextToBM(strbmName As String, oTemplate As Template, strAutotext As String)
'Graham Mayor - - Last updated - 17 Jul 2018
'strBMName is the name of the bookmark to fill
'oTemplate is the template with the autotext - probably ActiveDocument.AttachedTemplate
'strAutotext is the name of the autotext entry
Dim oRng As Range
On Error GoTo lbl_Exit
With ActiveDocument
Set oRng = .Bookmarks(strbmName).Range
Set oRng = oTemplate.AutoTextEntries(strAutotext).Insert _
(Where:=oRng, RichText:=True)
.Bookmarks.Add Name:=strbmName, Range:=oRng
End With
lbl_Exit:
Exit Sub
End Sub
Thanks

Anne

gmayor
11-28-2019, 10:26 PM
The short answer is no, however what you ask is possible if you change the concept a little.

Use the following macro to insert 11 bookmarks at the location where you want the selection of autotexts to be entered.


Sub InsertBMs()Dim oRng As Range
Dim i As Integer
Dim oBM As Bookmark
Set oRng = Selection.Range
oRng.Collapse 0
For i = 1 To 11
oRng.Text = "BM" & i
Set oBM = oRng.Bookmarks.Add("BM" & i)
Set oRng = oBM.Range
oRng.Collapse 0
oRng.Text = ChrW(8203)
oRng.Collapse 0
Next i
lbl_Exit:
Set oBM = Nothing
Set oRng = Nothing
Exit Sub
End Sub

Rename your AutoText entries BM1 to BM11 to reflect the bookmark names and the numbers of the checkboxes.

Use the following code in your useform. The code will fill the bookmarks with the appropriate autotext entry or a zero width space according to the check box selections. You can recall the userform and change the selections if you wish.


Option Explicit

Private Sub CommandButton1_Click()
Dim oCtrl As Control
Dim strName As String
Dim strText As String
Dim strNum As String
For Each oCtrl In Me.Controls
strName = oCtrl.Name
If strName Like "CheckBox*" Then
strNum = Replace(strName, "CheckBox", "")
strText = "BM" & strNum
If oCtrl.value = True Then
AutoTextToBM strText, ActiveDocument.AttachedTemplate, strText
Else
FillBM strText, ChrW(8203)
End If
End If
Next oCtrl
Unload Me
lbl_Exit:
Set oCtrl = Nothing
Exit Sub
End Sub


Private Sub AutoTextToBM(strbmName As String, oTemplate As Template, strAutotext As String)
'Graham Mayor - - Last updated - 17 Jul 2018
'strBMName is the name of the bookmark to fill
'oTemplate is the template with the autotext - probably ActiveDocument.AttachedTemplate
'strAutotext is the name of the autotext entry
Dim oRng As Range
On Error GoTo lbl_Exit
With ActiveDocument
Set oRng = .Bookmarks(strbmName).Range
Set oRng = oTemplate.AutoTextEntries(strAutotext).Insert _
(Where:=oRng, RichText:=True)
.Bookmarks.Add Name:=strbmName, Range:=oRng
End With
lbl_Exit:
Exit Sub
End Sub


Private Sub FillBM(strbmName As String, strValue As String)
'Graham Mayor - http://www.gmayor.com
Dim oRng As Range
With ActiveDocument
On Error GoTo lbl_Exit
Set oRng = .Bookmarks(strbmName).Range
oRng.Text = strValue
oRng.Bookmarks.Add strbmName
End With
lbl_Exit:
Set oRng = Nothing
Exit Sub
End Sub

Amich
12-05-2019, 09:42 AM
Hi Graham,

Works perfectly, thank you so much!

Anne