+ Reply to Thread
Results 1 to 3 of 3

Code to insert from a choice of building blocks into a Word document

  1. #1
    Registered User
    Join Date
    09-28-2010
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    19

    Code to insert from a choice of building blocks into a Word document

    Greetings all.

    To begin, a warning: Though I have dabbled a (very) little with Excel macros but am I am completely new to word macros.

    I have spent a long time developing a new work template in Word (2016) which includes a number of custom building blocks (in the QuickParts gallery). What I would like is for when a new document is created with this template for the user to be presented with a simple userform that offers a selection of some of these building blocks (say "BB01" and "BB02") and to insert whichever one is selected. So far I have managed to:
    • Create and test a simple macro which, if run manually, then inserts a predetermined building block (i.e. the name of the building block is hard-coded into the macro)
    • Create a simple userform with just three elements: a ComboBox, an "OK" command button and a "Cancel" command Button. The ComboBox currently offers a choice of two options and these are currently hard-coded into the userform and mirror the names of two of my building blocks). Clearly it would be more elegant for these choices to somehow generate automatically from the list of available building blocks (though I would only want a subset of these to show up), but for now I am happy to hard code them if that is easier.

    The bit I am now struggling with is connecting the dots between the above two, i.e. for the first macro to insert the chosen building block from the userform rather than the hard-coded one.

    I realise this is probably a very ambitious ask given my complete lack of VBA knowledge, but any pointers would be much appreciated. If my plea does prick your interest and you feel inclined to help, please explain things to me as if I were a five year old...

    Many thanks.

  2. #2
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Code to insert from a choice of building blocks into a Word document

    See: https://msdn.microsoft.com/VBA/Word-...uilding-blocks

    I assume you know how to populate a userform.

    Inserting the Quick parts from the userform can be as simple as having your userform call whichever of a number of macros inserts the building block corresponding to the choice that's been made. for example:
    Please Login or Register  to view this content.
    where "Choice 1" - "Choice 3" are the options display on a ListBox named ListBox1 on the userform and InsertQuickPart1 - InsertQuickPart3 are the macro names that do the Quick Part insertions.

    Populating the ListBox with only your Quick Parts would require you to have saved them to a custom category, so your userform can load only the names of the ones from that category. See 'Accessing an Existing Building Block' in the above link. If you populate the listbox with the names of your Quick Parts, those same names can be used for inserting them into the document. In this case you wouldn't need the Select Case processing of the above sub - you'd simply output Me.ListBox1.Value as the 'Name' parameter:
    Please Login or Register  to view this content.
    You might want something more nuanced than Selection.Range, however, for the destination. See 'Inserting a Building Block into a Document' in the above link.
    Cheers,
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    Registered User
    Join Date
    09-28-2010
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Code to insert from a choice of building blocks into a Word document

    Paul
    Many thanks for your reply. In fact, since posting my question I('ve almost) worked out a way to do what I wanted using check boxes instead of a ComboBox. I realised that would probably be better as it allows me to guide the user through a selection process, i.e. a first form with Option Buttons for Building Block Category A or Building Block Category B, then a second one which offers Option Buttons for the relevant blocks from whichever category has been chosen. I will, however, test out your solution, if only because I would like to know how to make that type of scenario work.
    In the meantime I have another issue now, which is that I cannot get the userform to display automatically as soon as a new document is created from the template, but I'm guessing I should post that separately.
    Many thanks for taking the time to help me.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. insert video in word document
    By scouse13 in forum Word Formatting & General
    Replies: 3
    Last Post: 01-25-2017, 09:47 AM
  2. Replies: 1
    Last Post: 01-23-2015, 03:16 PM
  3. need help Header/Footer Building blocks
    By rolta100 in forum Word Programming / VBA / Macros
    Replies: 20
    Last Post: 02-18-2014, 07:07 AM
  4. [SOLVED] Quick Parts making building blocks available to other computers
    By pierre86 in forum Word Formatting & General
    Replies: 1
    Last Post: 12-05-2013, 09:42 PM
  5. copying building blocks
    By Peeps32 in forum Word Formatting & General
    Replies: 3
    Last Post: 11-11-2011, 01:23 PM
  6. Insert a word document in another word document
    By Webtekr in forum Word Programming / VBA / Macros
    Replies: 1
    Last Post: 04-14-2009, 03:25 AM
  7. [SOLVED] Insert Excel hyperlink to word document
    By BHalberstater in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 04-26-2006, 04:00 AM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1