PDA

View Full Version : [SOLVED:] Code to insert a building block via a check box



jay1
03-08-2016, 03:37 AM
Hello, I am struggling to find the code to insert a building block using a tick box with a command button.

Also, im pretty new to this, does all the code go in the command button line? I would like to fill out the form with tickboxes and a few text fields then click ok when i am finished and it paste at the docvariables i have made?

heedaf
03-08-2016, 11:04 AM
Are you using Word or Access? Everything could go in a single sub. More detail on what you want to do would be very helpful.

gmaxey
03-08-2016, 03:30 PM
Since Jay posted in a Word forum, I'm going to assume he is using Word.

In this case all of the required code can go in the command button click event procedure. However, I wouldn't use DocVariable fields for building block content. I would use a bookmark or content control.




Private Sub CommandButton1_Click()
Dim oRng As Word.Range
If CheckBox1.Value Then
Set oRng = ThisDocument.AttachedTemplate.BuildingBlockTypes(wdTypeAutoText). _
Categories("Signatures").BuildingBlocks("George Washington").Insert(ActiveDocument.Bookmarks("bmDemo").Range, True)
ActiveDocument.Bookmarks.Add "bmDemo", oRng
End If
Hide
End Sub

jay1
03-14-2016, 04:48 AM
Hello,

Thanks so much for that gmaxey it works perfect for one box, can't seem to get a second box to work says block if without end if

Not sure what i have done wrong, can you provide some insight?

Thanks again


This is the code i have been pra



Private Sub CommandButton1_Click()
Set ovars = ActiveDocument.Variables


Dim oRng As Word.Range
If CheckBox1.Value Then
Set oRng = ThisDocument.AttachedTemplate.BuildingBlockTypes(wdTypeAutoText). _
Categories("general").BuildingBlocks("electrical").Insert(ActiveDocument.Bookmarks("d").Range, True)
ActiveDocument.Bookmarks.Add "d", oRng

If CheckBox2.Value Then
Set oRng = ThisDocument.AttachedTemplate.BuildingBlockTypes(wdTypeAutoText). _
Categories("general").BuildingBlocks("4.2").Insert(ActiveDocument.Bookmarks("bmdemo").Range, True)
ActiveDocument.Bookmarks.Add "bmDemo", oRng

End If
Hide






Me.Hide
ovars("customername").Value = Me.TextBox1.Value
ovars("line1").Value = Me.TextBox2.Value
ovars("line2").Value = Me.TextBox3.Value
ovars("line3").Value = Me.TextBox4.Value



ActiveDocument.Fields.Update
'Clear the variable
Set ovars = Nothing
'Unload the form
Unload Me
End Sub

gmaxey
03-14-2016, 05:15 AM
You have 2 If and only 1 End If

End the first If

DaRichMan
06-20-2022, 02:18 AM
I new to VBA and trying to get this to work, Tried to change some of the above but keep getting errors.

I have a userform where a name has to be filed in and a date. And depending on which radio button has been checked building block 1 of 2 is placed in the bookmarks spot.

gmaxey
06-20-2022, 06:30 AM
Unless it is a state secret, you should post your code.

Paul_Hossler
06-20-2022, 08:21 AM
I new to VBA and trying to get this to work, Tried to change some of the above but keep getting errors.

I have a userform where a name has to be filed in and a date. And depending on which radio button has been checked building block 1 of 2 is placed in the bookmarks spot.

Since this is an 6 year old [SOLVED] thread, it's probably better to start a new one and maybe just reference this one

If you can post a small example including any code that you have, it usually makes things easier