Consulting

Results 1 to 8 of 8

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

  1. #1
    VBAX Newbie
    Joined
    Mar 2016
    Location
    essex
    Posts
    5
    Location

    Code to insert a building block via a check box

    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?

  2. #2
    VBAX Contributor
    Joined
    Jun 2014
    Posts
    107
    Location
    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.

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

    Visit my website: http://gregmaxey.com

  4. #4
    VBAX Newbie
    Joined
    Mar 2016
    Location
    essex
    Posts
    5
    Location
    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

  5. #5
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    You have 2 If and only 1 End If

    End the first If
    Greg

    Visit my website: http://gregmaxey.com

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

  7. #7
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    Unless it is a state secret, you should post your code.
    Greg

    Visit my website: http://gregmaxey.com

  8. #8
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,711
    Location
    Quote Originally Posted by DaRichMan View Post
    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
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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