dLSoft  barcode & labelling software     

Barcodes in Word

Generating barcodes in Word documents may be achieved using any of the several methods provided in Barcodes & Labels for Office. One technique is to use an Active control, where the barcode data and properties may be set using a pop-up dialog box. Alternatively barcodes in Word may be created using macros. Barcodes & Labels for Office include four macros called PBarcode and FBarcode (which create a barcode from the text selected when the macro is called) and PBarcodeSetup and FbarcodeSetup (which initialise or change the barcode properties). Yet another is to use Insert Object to insert a barcode image. Suitable for Word 2007, 2010 or later.

Active barcodes in Word

An Active Barcode Control may be placed on a Word document by selecting the Controls toolbox on the Developer ribbon.

When the Control toolbox is displayed select More controls from the Active-X Controls section (the bottom-right icon). This opens the More Controls dialog which lists the available Active-X controls.

Select Active Barcode Control (or the Active 2D-Barcode Control for 2D barcodes) and push the OK button [or double-click on the Active Barcode Control entry].

select active barcode control in Word

A default barcode image is placed on the document. The properties of the barcode may be modified in either of two ways, both of which involve selecting the barcode – and this can only be done in Design Mode, so if clicking on it once does not result in the image being selected, select the Design Mode icon on the Developer ribbon and try again.

Once selected, right click on the selected barcode and choose Active Barcode Control Object from the pop-up menu displayed. A submenu appears. Select Properties from this menu and  the Active Barcode Control Properties dialog is displayed; this allows all features of the barcode to be specified.

Barcode control in Word

Barcode properties dialog

Alternatively you can use Word’s Properties box by right-clicking on the image and selecting Properties from the pop-up menu. This results in the display of Word’s Properties dialog for the control where, again, all properties may be specified.

 

Barcode in Word    Click to enlarge

Creating barcodes in Word using macros

Barcodes & Labelling for Office includes macros for generating font based or picture barcodes in a Word document.

The PBarcodeSetup and PBarcode macros are for picture barcodes, while the FBarcodeSetup and FBarcode macros are for font barcodes.The macros can be installed into any template, including the Normal template - which makes the macros available for any new Word document created.

PBarcodeSetup or FBarcodeSetup must be run at least once to set the barcode properties before the PBarcode and FBarcode macros can be used

barcode setup dialog for picture barcodes Picture barcodes properties dialog

barcode setup dialog for font barcodes Font barcodes properties dialog

Whenever you wish to create a barcode simply select the text you need to turn into a barcode then run either the PBarcode macro (if you want a barcode picture) or FBarcode (if you want a font-based barcode) from the list of macros in the Macros dialog.

If you use a macro frequently you may wish to create a key shortcut for the macros concerned.

For Office 2007 choose Word Options from the Office button, select Customize and scroll down the left-hand list of categories to select Macros.

For Office 2010/2013 choose Options from the File menu, select Customise Ribbon, and push the Keyboard shortcuts Customise button, then scroll down to Macros.

Select one of the macros. Place the cursor into the “Press New Shortcut Key” box and press the key you would like to use as a shortcut for this macro (e.g. the F11 key). Then push the Assign button. Finally push the Close button.

Now whenever you select some text and push the appropriate shortcut key, the selected text will be replaced by a barcode.

Customisation of the Word macros

The PBarcode macro replaces selected text with a barcode picture generated from that text using the code.

St$ = Selection.Text
If Len(St$) < 1 Then
    MsgBox ("Please select text required for barcode")
    GoTo done
    End If

pa = 0
er1 = BarCodew(St$, pa)
If er1 = 0 Then
    WordBasic.EditPasteSpecial DataType:="PICT"
Else
    If er1 = 1 Then
        x$ = "Wrong number of characters in barcode"
    ElseIf er1 = 4 Then
        x$ = "Illegal character in barcode data"
    Else
        x$ = "Error in barcode data"
    End If
    MsgBox (x$)
End If

This code pastes the barcode picture into the existing selected text – thus replacing the text. The macro can easily be edited to past the picture elsewhere if required, just generating another Selection or cursor point before the WordBasic.EditPasteSpecial DataType:="PICT" line.

The Fbarcode macro replaces selected text with other text returned by the DLL and then selects the new text and changes its font to the current barcode font settings:

St$ = Selection.Text
If Len(St$) < 1 Then
    MsgBox ("Please select text required for barcode")
    GoTo done
    End If
pa = 0
so$ = String(200, " ")
fo$ = String(36, " ")
er1 = BarCodewF(ByVal St$, pa, ByVal so$, ByVal fo$, fht)
If er1 > 0 Then
    Selection.Text = Left(so$, er1)
    Selection.Font.Name = fo$
    Selection.Font.Size = fht
Else
    If er1 = 1 Then
        x$ = "Wrong number of characters in barcode"
    ElseIf er1 = 4 Then
        x$ = "Illegal character in barcode data"
    Else
        x$ = "Error in barcode data"
    End If
    MsgBox (x$)
End If
done:

Again the barcode text returned in Left(so$,er1) can be inserted elsewhere if required.

Using Mail-merge to print barcodes in Word

Note: If you wish to print labels/cards/tickets using data from Access or Excel you can use the label printing facility in Barcodes & Labels for Office; see printling Labels from Access or Labels from Excel

Word's mail-merge is suitable only for font-based barcodes. The merge data must already exist contain the correct characters to form the barcode when displayed in the barcode font. While the data can come from any source this description assume that the data is in an Excel spreadsheet and that the font-based barcodes within the spreadsheet have already been created using the FBarcode macro within Excel see Barcodes in Excel.

A sample mail-merge group of files is provided in the mailmerge subdirectory of the installation directory. We recommend that you create a new directory c:\mergesample and copy the merg2k.xls spreadsheet from the mailmerge subdirectory to the new directory if you wish to follow the instructions below to generate a trial mail-merge.

1. These instructions assume that the directory is c:\mergesample\ If you chose another directory please adjust the instructions accordingly.

2. Start Word and select Mail merge from the Tools menu.

3. You are present with a three-step wizard. Step 1 is "Create Main Document". Push this button, and select the type of document you require.  (suggest Active Document)

4. Push the Step 2 button to Get Data from the data source. Chose open data source, choose MS Excel Worksheets from the document type listbox and then navigate the File Open dialog to find C:\mergesample\merg2k.xls. Push the Open button.

5. Accept the Entire spreadsheet question box.

6. Push the Setup Main Document button displayed. Then select the form/label type you require. Push the OK button.

7. The Create form/label dialog is displayed. Push the Insert Field button and select the first field (M_123456) from the drop-down list of fields, then push the OK button. Place the cursor at the end of this field and push the Enter key twice.

8. Select the second field (UJEfbZVYZeBfP - this is actually the barcode) from the drop-down list of fields then push the OK button.

9. Select the <<UJEfbZVYZeBfP>> characters INCLUDING THE << AND >> CHARACTERS in the dialog box so that they are highlighted. Right-click on the highlighted text and choose Font from the menu displayed. Select UNBW font (or one of the other UN fonts you have installed) and choose a size of 36 point. Push the OK button.

10. Now push the OK button.

11. Now at Step 3 in the Wizard, push the Merge Fields button. A dialog is displayed which enables you to decide whether to merge straight to the printer or to a new Word document. Make your choice (suggest new document as there are 30 records in the sample data).  Push the Merge button.

That's it. You should either have a screen display or a printed output showing the records, each with a barcode and a number.

A sample document (Mailmerg.doc) is included in the mailmerge subdirectory of the product's installation directory. This is a mail-merge master document created as described above.