Add Barcodes to Word Labels or a Mail Merge Document

A typical application for B-Coder is to either add barcodes to mailing labels or to insert barcodes into a mail merge document. The easiest way to do this is to use the Mail Merge tool in Word to create your merge documents or labels and then use B-Coder to add the barcodes to the final merged documents containing all of your merge data.

You would first set up your mail merge in Word as normal using the mail merge tool. After you set up your data source and insert your “merge fields” in the main merge document, you could select the merge field (or any static text) in your main merge document that you want converted to a barcode and change its style to a new style named “barcode”.

(To create a new style, open the “Format” menu, select “Style” and then click the button marked “New” in the style dialog box. In the “New Style” dialog box, enter the name “barcode” and click the OK button.)

Next, you would complete your mail merge as normal by selecting “Merge to New Document”. This will produce a final merge document with all of the data from the data source inserted into your labels or documents. Any merge fields or static text that you assigned the style “barcode” to in your main merge document would also have the style “barcode” in the final merge document. To fill in the barcodes, you could simply run a macro that searches through the final merge document looking for any text with the style “barcode” and replaces that text with a barcode created by B-Coder. The following page contains an example macro that does just this.

The following WordBasic macro (for Word 6 and Word 7) searches through a document looking for any text with the style “barcode”. If it finds any, it converts the text to a barcode.

 Sub MAIN()
 ' run B-Coder Minimized
  Shell "C:\B-CODER\B-CODER.EXE", 6 
  ' Note: make sure that the path for B-Coder is 
  'correct in the above command

  ' open DDE link to B-Coder
  Chan = DDEInitiate("B-Coder", "System") 
  
  ' initialize B-Coder here - send dde commands to set up your Bar Codes
  ' DDEExecute Chan, "[Code128]" ' tell B-Coder to use Code 128
  ' DDEExecute Chan, "[messagewarnings=off]" ' turn off warning messages
  ' DDEExecute Chan, "[printwarnings=off]" 
  ' DDEExecute Chan, "[height=.5]" ' set bar code height
  
  StartOfDocument ' move to top of document
  On Error Goto bye ' quit if an error occurs
  
  While 1 = 1 ' loop forever
  EditFindStyle .Style = "BarCode" ' find style "BarCode"
  EditFind .Find = "", .WholeWord = 0, .MatchCase = 0, _
    .Direction = 0, .Format = 1
  If EditFindFound() <> - 1 Then Goto bye ' if not found then quit
  
  BCData$ = Selection$() ' otherwise get bar code data
  
  If Right$(BCData$) = Chr$(13) Then BCData$ = Left$(BCData$, Len(BCData$) -1)
  ' the above line removes the carriage return at the end of the selected text
  
  'generate a bar code
  DDEExecute Chan, "[BARCODE=" + BCData$ + "]" 
          
  ' ----------- option 1 - replace the text with a bar code
  EditClear 'delete the current selection
  ' option 1 works best when adding bar codes to a Merge Document
  ' ----------- option 2 - put the bar code below the text
  ' EndOfLine ' move to end of line
  ' LineDown 1 ' move down one line
  ' StartOfLine ' move to the start of this line
  ' option 2 works best when adding PostNET or other bar codes to mailing labels
  ' you may need to change the above three lines to get the bar code where you
  ' need it
  
  EditPaste ' paste in the bar code
  Wend ' go find the next one

bye: ' all done
  ' tell B-Coder to quit
  DDEExecute Chan, "[Appexit]" 
  On Error Goto 0 ' remove error trap
End Sub

Contact Us