PDA

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



DaRichMan
07-12-2022, 12:16 AM
Regarding [SOLVED] Code to insert a building block via a check box (vbaexpress.com)

I'm trying the following
29949

If Mobile is Yes then Building block With mobile phone is inserted at Bookmark MainText
If Mobile is No then Building block Without mobile phone is inserted at Bookmark MainText



Private Sub CancelButton_Click()
UserInfo.Hide
End Sub

Private Sub OkButton_Click()
Set ovars = ActiveDocument.Variables
Dim oRng As Word.Range
If OptionButton1.Value Then
Set oRng = ThisDocument.AttachedTemplate.BuildingBlockTypes(wdTypeAutoText). _
Categories("general").BuildingBlocks("With mobiel phone").Insert(ActiveDocument.Bookmarks("MainText").Range, True)
ActiveDocument.Bookmarks.Add "MainText", oRng
End If
If OptionButton2.Value Then
Set oRng = ThisDocument.AttachedTemplate.BuildingBlockTypes(wdTypeAutoText). _
Categories("general").BuildingBlocks("Without mobiel phone").Insert(ActiveDocument.Bookmarks("MainText").Range, True)
ActiveDocument.Bookmarks.Add "MainText", oRng
End If
Hide
Me.Hide
ActiveDocument.Fields.Update
Unload Me
End Sub


I'm getting Error 591.
Any ideas?

As an extra I would like to have the oportunity to insert an image (if possible) and have it inserted at Bookmark space Image.

gmaxey
07-13-2022, 01:57 AM
Works fine here is the building block is actually named "With mobiel phone" where "mobiel" is probably as spelling error. Otherwise I get an error 5941.

DaRichMan
07-13-2022, 02:16 AM
Mobiel was indeed a spelling error, but still can't get it to work.

Maybe it has to do with the language version? I use Dutch.

29958

gmaxey
07-13-2022, 02:54 AM
Nope. Two problems:

1. Your building blocks are stored in the Textboxes gallery and your code is looking for them in the AutoText gallery.
2. You have one of your building blocks named "With mobile Phone" and the code is looking for "With mobile phone"

DaRichMan
07-13-2022, 04:12 AM
Nope. Two problems:

1. Your building blocks are stored in the Textboxes gallery and your code is looking for them in the AutoText gallery.
2. You have one of your building blocks named "With mobile Phone" and the code is looking for "With mobile phone"

OMG, It works. Did not know it was case sensitieve.

Thank you!

gmaxey
07-13-2022, 09:18 AM
You are welcome.