Share via

Inserting Building Blocks using UserForm

Anonymous
2014-11-24T08:11:42+00:00

Hello again,

I have another question regarding another question I had earlier on.

I got this code from Greg Maxey (thank you very much!):

Private Sub CommandButton1_Click()

'A basic Word macro coded by Greg Maxey

Dim oBm As Bookmark

Dim oRng As Word.Range

  Set oBm = ActiveDocument.Bookmarks("bmTarget")

  Set oRng = NormalTemplate.BuildingBlockTypes _

    (wdTypeAutoText).Categories("General").BuildingBlocks(Me.ComboBox1.Value).Insert(oBm.Range)

  ActiveDocument.Bookmarks.Add "bmTarget", oRng

  Me.Hide

End Sub

I wanted to have a userform with a single drop down (combobox), that asks the user to choose between "option1" and "option2". The bookmark in the document is the same, but the building block will either be "BBoption1" or "BBoption2", depending on what was chosen in the combobox.

Wouldn't there need to be some part of the code representing that? So that one part puts the "BBoption1" in the document and one puts "BBoption2" in it?

Thank you very much!!!

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2014-11-24T14:19:55+00:00

Where do you have your code?

This should be in the ThisDocument module:

Private Sub Document_Open()

UserForm1.Show

End Sub

The rest should be in the userform module.

Was this answer helpful?

0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Anonymous
    2014-11-24T13:52:36+00:00

    Hey

    It doesn't open the UserForm when I open the document and if then manually "run" it, all it does is open VBE (while inserting the BB)? I just don't understand, why it always opens VBE also?

    It's saved as .dotm. And I don't get an error message, it's just not working for some reason.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2014-11-24T13:16:52+00:00

    Mel

    Your code works fine here.  What problem (error are you getting)?

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2014-11-24T12:06:26+00:00

    Unfortunately it doesn't. This is all the code I'm using (am I missing something?).

    Private Sub CommandButton1_Click()

    Dim oBm As Bookmark

    Dim oRng As Word.Range

      Set oBm = ActiveDocument.Bookmarks("bmTarget")

      Set oRng = NormalTemplate.BuildingBlockTypes _

        (wdTypeAutoText).Categories("General").BuildingBlocks(Me.ComboBox1.Value).Insert(oBm.Range)

      ActiveDocument.Bookmarks.Add "bmTarget", oRng

      Me.Hide

    End Sub

    Private Sub Document_Open()

    UserForm1.Show

    End Sub

    Private Sub UserForm_Initialize()

        With ComboBox1

            .AddItem "BBoption1"

            .AddItem "BBoption2"

        End With

    lbl_Exit:

      Exit Sub

    End Sub

    My bookmark is called bmTarget, the BB are AutoText, category General, and in the "normal" template.

    Thank you!

    Was this answer helpful?

    0 comments No comments
  4. Doug Robbins - MVP - Office Apps and Services 323.1K Reputation points MVP Volunteer Moderator
    2014-11-24T09:44:16+00:00

    If you have the "BBoption1" and "BBoption2" in the combobox, then the Me.ComboBox1.Value will take care of it.

    Was this answer helpful?

    0 comments No comments