Questions? Search our site or call us at 1-800-722-6004

Insert Barcodes into the Header or Footer of a Document

Make sure you enable the macros when opening the document. In Word 2000 also make sure that the macro security level is set to medium. (Check under Tools|Macro|Security).

  1. Copy the Sub InsertBarCodes() Routine to the clipboard by highlighting it and pressing Ctrl + C.
  2. In Microsoft Word, Open the Visual Basic Editor by pressing Alt + F11
  3. Insert a new Module, by clicking on Insert|Module
  4. Paste the code into the module
  5. Run the macro by going to the "Tools" menu, selecting "macro" and clicking on "Macros" and then double clicking on "InsertBarCodes" from the list.

Sub InsertBarCodes()
  ' InsertBarCodes Macro
  ' Macro written 6/28/01

  Dim YouWantThisSubToLaunchBCoder
  Dim BCoderPath$
  Dim BCPath$
  Dim Chan
  On Error Resume Next ' ignore errors
  'Delete barcode image file if it exists
  Kill ("C:\barcode.wmf")
  If Application.Documents.Count < 1 Then
    MsgBox "This macro cannot be run with no documents open!"
    GoTo Quit
  End If
  YouWantBCoderToExitWhenDone = 0
  ' change the above line to:
  ' "YouWantBCoderToExitWhenDone  = 1"
  ' to have B-Coder quit running at the end of this macro
  ' check if B-Coder path is registered in WIN.INI
  BCoderPath$ = System.ProfileString("B-Coder", "ExePath")
  If Len(BCoderPath$) = 0 Then
    ' if path isn't registered then assign the path here
    BCoderPath$ = "C:\B-Coder3\B-Coder.Exe"
    ' Change the path in the above statement to point to
    ' your copy of B-Coder.
    ' add path to Win.ini
    System.ProfileString("B-Coder", "ExePath") = BCoderPath$
    'B-Coder path is in the WIN.INI - allow automatic launching
    YouWantThisSubToLaunchBCoder = 1
  End If
  If Not Tasks.Exists("B-Coder Professional") Then
    ' If B-Coder is already running then do not launch
    Shell BCoderPath$, 2
    ' The above line launches B-Coder with its window minimized
    ' The ,2 is part of the Shell statement in VBA that
    ' indicates to run the application minimized.
	' set focus back to Word 
    Tasks("Microsoft Word").Activate
  End If
  'If Bcoder path is incorrect then warn user
  If Tasks.Exists("B-CODER Professional") = 0 Then 
    ' Check if B-Coder is running
    MsgBox "B-Coder is not running! - B-Coder must be running either in a
    window or as an icon for this macro to work. Please start B-Coder and try again."
    GoTo Quit
  End If
  'Get Message to encode
  BCData$ = InputBox("enter a bar code message:")
  ' remove any carriage return/line feeds 
  ' from the selected text
  If Right$(BCData$, 1) = Chr(13) Then
    BCData$ = Left$(BCData$, Len(BCData$) - 1)
  ElseIf Right$(BCData$, 1) = Chr(10) Then
    BCData$ = Left$(BCData$, Len(BCData$) - 2)
  End If
  ' strip off trailing spaces
  BCData$ = RTrim$(BCData$)
  ' if no text is selected then quit
  If Len(BCData$) = 0 Then GoTo Quit
  ' Initiate DDE link
  Chan = DDEInitiate("B-Coder", "System") 
  ' Turn off print warnings/message warnings
  DDEExecute Chan, "[PrintWarnings=off]" 
  DDEExecute Chan, "[MessageWarnings=off]" 

  ' Set B-Coder to Code 39
  DDEExecute Chan, "[CODE39]"
  ' Set height to quarter inch 
  DDEExecute Chan, "[BARHEIGHT=0.25]" 
  ' generate the bar code
  DDEExecute Chan, "[barcode=" + BCData$ + "]" 
  'Save the barcode to disk
  DDEExecute Chan, "[SAVEBARCODE(C:\barcode.wmf)]"
  ' Terminate the DDE link
  DDETerminate Chan 
  'open header/footer view
  ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
  'Use the following line to insert 
  'the barcode into the Header:
  'ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
  'Delete last barcode if exists
  Selection.Delete Unit:=wdCharacter, Count:=1
  'insert barcode image
  Selection.InlineShapes.AddPicture FileName:="C:\barcode.wmf", _
  LinkToFile:=False, SaveWithDocument:=True
  'close header/footer view
  ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
  End Sub

Categories: Macro / Code Sample, Microsoft Word, Barcode Generation, Standalone Desktop Application, B-Coder

Last Updated: 2013.08.30

Need more help?

Don't hesitate to call or email us with your questions

Our office is open 9AM - 5PM Monday Through Friday (E.S.T.)

Technical Support: 215-496-0202

Toll-Free: 1 (800) 722-6004
Skype: taltech1 (Voice only)