fragments appear as part of main menu

Creating and using code fragments

Code fragments (synonym: snippets) are pieces of code that occur regularly in programs and for that reason have been collected for reuse. Code VBA includes a code fragment library which can be extended by you with complete procedures or a couple of lines of code.

Inserting a fragment

Code fragments generally appear in the Fragments sub menu of an object. In some special cases they appear at directly in the main menu - the image on the right shows such a case for VBA »Manage File. Code fragments can be inserted using the first character or using down-arrow and Enter or using the mouse. Depending on the number of variable involved in the fragment, the code is inserted directly (0 variables), a submenu may appear letting you select a candidate value or variable (1 variable), or a fragment inserter dialog opens allowing you to specify multiple values.To see what a fragment contains either move to it using the cursor and press F1 or hover the cursor above it.

Tooltip showing fragment code

Click this Start screencast showing fragments being inserted to see how to add fragmentsInsert fragment screencast

You may also want to extend the library with your own fragments. This is discussed in the following sections.

Fragments are files stored in a \My Documents\VBA Code\ subfolder - as an example the fragments on the right image are located in \My Documents\VBA Code\VBA\Manage File\. They can be normal procedures in standard code modules (.bas, .frm, .cls) or snippets stored in .vba fragment files.

Adding standard modules

To add a module containing procedures to a suitable location in the fragments library:

  1. Determine the location where adding it is most appropriate - below
  2. Use the VBE built in Export feature menu: File » Export File (or CRTL-E).
  3. In the dialog that pops up browse to the folder you identified in step 1. and press Save
All public procedure in a module inserted in a fragments subfolder appear in the menu. By make certain procedures Private you can hide them.

Adding .vba fragments

When you intend to create a code fragment of your own, the first question is where will be the best place to store it. What location in the menu will be most likely to look for it. Here are some suggestions:

  1. If a fragment is generally used in all Office applications, then put it in the most appropriate subfolder of the VBA menu
  2. If it is specific for a certain class in an Office applications, add it to that class's subfolder. In the above, fragments appearing under ActiveCell are stored in the Range subfolder of Excel.
  3. If it concerns a generally useful function like IsValidSheetname, store it under the Application class subfolder of Excel. It will then be available from the (Excel) Application menu

After you decided what is the best location to put it, following the menu to the selected point and press Manage Fragments

.vba Fragment syntax

A fragment is essentially just some VBA code, optionally with some fragment tags.

The example fragment below illustrates things you can do with fragments. Press < to open a selection list of available tags. Select an item and press Enter.

fragment syntax

<cursor>; lets you determine where the cursor should appear after the fragment is inserted. If not specified the cursor will be at the end of the fragment

<Date> Tells the fragment insertion to try to find a variable of the selected type (here Date) or insert a new variable. If more than one variable of this type is found only a prefix is inserted, allowing the user to use intellisense to select the appropriate variable

<String:File> Adding a name after : inside the tag allows you to propose a filename in case a new variable is to be created - in this example the variable name will be strFile.

<Range> Alternatively, you can construct a tag with a typename yourself.

<include> An advanced use allows you to specify a module, form or class to be inserted when using the fragment. In the case below the module contained the Windows API call for getting the Temp folder.

<select.Action:.Display,.Send,.Save> let the user select for field Action one of three options. Can only be used in fragments with multiple variables.

a sofisticated fragment with options

Managing code fragments

In addition to creating fragments you will also want to edit, copy or remove them. You will be able to do all these using Windows Explorer. Using the Manage Fragments on the menu where the fragment you want to to work on opens Explorer in the correct folder location.

Edit an existing fragment

To open an existing fragment for editing

  1. In the menu go to where the fragment is located
  2. Select Manage Fragments - use shortcut M
    Windows Explorer opens in the folder containing the fragment
  3. Open the fragment using double-click or right-click » Open

Click this How to open an existing fragment for editing screencast to see how to open an existing fragment for editing open fragment screencast

comment menu

Customize the procedure header and change comment fragment

Two fragments you are very likely to want to customize are the 'Procedure Header' and 'Code Change' fragments. These can be found under the Comment menu.