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
- Adding standard modules
- Adding .vba fragments
- .vba Fragment syntax
- Managing code fragments
- Edit an existing fragment
- Customize the procedure header and change comment fragment
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.
Click this to see how to add fragments
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:
- Determine the location where adding it is most appropriate - below
- Use the VBE built in Export feature menu: File » Export File (or CRTL-E).
- In the dialog that pops up browse to the folder you identified in step 1. and press Save
Tip |
---|
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:
- If a fragment is generally used in all Office applications, then put it in the most appropriate subfolder of the VBA menu
- 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.
- 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.
<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.
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
- In the menu go to where the fragment is located
- Select Manage Fragments - use shortcut M
Windows Explorer opens in the folder containing the fragment - Open the fragment using double-click or right-click » Open
Click this to see how to open an existing fragment for editing
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.