INFO: VBA Example to Use Barcode Functions in MS Word

SUMMARY

This article provides some information on converting selected text into barcodes using barcode fonts and VBA functions.

MORE INFORMATION

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.

Creating a Macro

  1. Open you document (referred as barcode.doc in this tutorial). Pop up Macros dialog with ToolsMacroMacros (ALT+F8). On Word 2007, first switch to Developer toolbar by clicking on Developer menu, then click on Macros tool button.

  2. In the Macros, enter name CreateUPCBarcode and choose Create.

  3. 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 function UPC_A to convert selected number into barcode string. After that, it overwrites the current selection with barcode string.

  4. 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 Import File. Navigate to the folder c:\program files\common files\morovia\moroviafonttools and locate moroviafonttools.bas file. Click OK. Make sure that you only import this file once.

Applying Macro

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 ToolsMacroMacros (Alt+F8). Select CreateUPCBarcode and click on Run. The highlighted number becomes a UPC-A barcode.

Assigning Shortcut to Macro

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.

  1. 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 Customize command (Alternative: ToolsCustomize).

  2. Choose the Command tab in the Customize dialog box. Choose the Keyboard... button at the bottom of the Customize dialog box.

  3. 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 the CreateUPCBarcode macro.

  4. 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 Assign button.

  5. 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.

Assigning Menu Item to Macro

You can also assign a menu item to the new macro.

  1. 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 Customize command (Alternative: ToolsCustomize).

  2. 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 find CreateUPCBarcode macro (it's listed as Project.Module1.CreateUPCBarcode).

  3. Drag the macro icon (it looks like a small organization chart or flowchart) to the Edit menu. Word opens the Edit menu.

  4. 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.

  5. 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.

  6. In the Customize dialog box, choose Close.

To undo the change, use the ToolsCustomize, then click on the menu item (to select it) and right-click your mouse and choose the Delete command.

Deleting Macro

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.

APPLIES TO

  • Morovia Linear Barcode Fontware

  • Microsoft Word