This article provides some information on converting selected text into barcodes using barcode fonts and VBA functions.
In the first part of this tutorial, we'll demonstrate how you can create a macro that turns the selected text into a UPC-A barcode. Later on, we'll show you how to associate the macro with a keyboard shortcut or menu item so that you can invoke the macro by pressing a key or selecting a menu item.
In order to use this tutorial, you need to have Microsoft Word 2000 and above and have the Macro enabled. You will also need Morovia UPC/EAN/Bookland Fontware. If you have other linear barcode fonts package, substitute the font name and encoder function with the ones in your package. You do not need the full version - the trial version will do just fine.
Macro can be in global scope or in document scope. If a macro is placed
into normal.dot
template, which is loaded into every
Word document, it is visible to every document. Otherwise it resides on
the specific document you are working on. In this tutorial we place macro
into the document we are working on. If you need to reuse the macro frequently
you may consider to place into normal.dot
template to
make it global.
Open you document (referred as
barcode.doc
in this tutorial). Pop up Macros dialog with → → (ALT+F8). On Word 2007, first switch to Developer toolbar by clicking on menu, then click on Macros tool button.In the Macros, enter name
CreateUPCBarcode
and choose .Now Visual Basic Window appears with the definition of subroutine
CreateUPCBarcode
. The VBA editor created the function and the body is left to fill out. Enter code in the body, to make the whole definition looks like below:Sub CreateUPCBarcode() Selection.Font.Name = "MRV UEBMA" Selection.Font.Size = 12 Selection.TypeText (UPC_A(Selection.Text)) End Sub
The code first changes the font of selected text to
MRV UEBMA
, then calls functionUPC_A
to convert selected number into barcode string. After that, it overwrites the current selection with barcode string.Before we close the editor, we need to import Morovia VBA module because the macro calls an encoder function
UPC_A
which is defined in Morovia Font Tools module. Right click on the Project (barcode.doc) node and select . Navigate to the folderc:\program files\common files\morovia\moroviafonttools
and locatemoroviafonttools.bas
file. Click . Make sure that you only import this file once.
After the macro is created, it can be applied manually. In
the document, type our UPC number 01234567891
and
highlight it. Pop up Macros dialog with → → (Alt+F8). Select CreateUPCBarcode
and click on . The highlighted number
becomes a UPC-A barcode.
After the macro is defined, you can assign the macro to a toolbar button, or a keyboard shortcut. Let's assign our new macro shortcut Ctrl+Alt+B.
Right-click within the gray area at the top of Word –- inside the menu bar, within the toolbar (but not on a button), or in the gray, empty area to the right of the menus and toolbar buttons. Choose the
command (Alternative: → ).Choose the Command tab in the Customize dialog box. Choose the button at the bottom of the Customize dialog box.
In the Save changes in pull-down menu at the bottom, choose the current document
barcode.doc
. In the Categories list, choose Macros, and in the right panel you'll find theCreateUPCBarcode
macro.Click inside the Press new shortcut key text box. Press the shortcut key you'd like to use, such as
Ctrl+Alt+B
(this combination will appear in the text box). If the key combination hasn't been assigned to another command, Word displays Currently assigned to: [unassigned]. Click on the button.The shortcut appears in the Current keys list. Dismiss all dialogs.
To undo the change, use the Tools/Customize command, then right-click the button and choose Delete. Alternatively, hold down the Alt key and drag the icon off the toolbar.
You can also assign a menu item to the new macro.
Right-click within the gray area at the top of Word –- inside the menu bar, within the toolbar (but not on a button), or in the gray, empty area to the right of the menus and toolbar buttons. Choose the
command (Alternative: → ).Choose the Command tab in the Customize dialog box. In the Save in pull-down menu at the bottom,
choose barcode.doc
. In the Categories list, choose Macros, and in the right panel you'll findCreateUPCBarcode
macro (it's listed asProject.Module1.CreateUPCBarcode
).Drag the macro icon (it looks like a small organization chart or flowchart) to the Edit menu. Word opens the Edit menu.
Move down the Edit menu. Your mouse cursor changes into a button, and as you move a dark vertical line appears in the menu at the position where you will be "dropping" (inserting) the new command. When you are satisfied with the location, release the mouse, dropping the line in the desired position (see Figure 5). The line changes to text: Word displays the Macro name. I chose to add the command between the Replace and the Go To commands.
The Edit menu remains displayed. Right-click on your macro and change the name. I chose
UPC-A &Barcode
. This allows me to press Alt+E (to open the Edit menu), then press B to execute the macro.In the Customize dialog box, choose Close.
To undo the change, use the
→ , then click on the menu item (to select it) and right-click your mouse and choose the command.When you save barcode.doc
, your toolbar,
menus, and keyboard
shortcuts are saved with your document. Since we assigned the changes
to barcode.doc
, rather than normal.dot
,
the changes will be reflected
only in barcode.doc
. Open any other document
(or create a new one) and
your menus, toolbars, and keyboard shortcuts won't reflect the
shortcut changes you made in barcode.doc
.