Consulting

Results 1 to 6 of 6

Thread: Code to insert a building block via a check box

  1. #1

    Code to insert a building block via a check box

    Regarding [SOLVED] Code to insert a building block via a check box (vbaexpress.com)

    I'm trying the following
    Schermafbeelding 2022-07-12 090831.png

    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.
    Last edited by Aussiebear; 07-12-2022 at 12:48 AM. Reason: Reduced whitespace

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    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.
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    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.

    Form.zip

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    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"
    Greg

    Visit my website: http://gregmaxey.com

  5. #5
    Quote Originally Posted by gmaxey View Post
    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!

  6. #6
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    You are welcome.
    Greg

    Visit my website: http://gregmaxey.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •