 |
Insert Bar Codes into the Header or Footer of your Document
Background:
This article describes how to use B-Coder to insert a barcode image
into the header or footer of a MS Word 97/2000 document.
Solution:
To use the code below Download
a fully working example of this macro and jump to step 5 or
if you are confident with Word VBA Programming:
-
Copy the Sub InsertBarCodes()
Routine to the clipboard
by highlighting it and pressing Ctrl + C.
- In Microsoft Word, Open
the Visual Basic Editor by pressing Alt + F11
- Insert a new Module, by
clicking on Insert|Module
- Paste the code into the
module
- 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
Beep
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$
Else
'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.
Tasks("Microsoft Word").Activate
' set focus back to Word
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
Chan = DDEInitiate("B-Coder", "System") '
Initiate DDE link
DDEExecute Chan, "[PrintWarnings=off]" '
Turn off print warnings
DDEExecute Chan, "[MessageWarnings=off]" '
Turn on message warnings
DDEExecute Chan, "[CODE39]" ' Set B-Coder
to Code 39
DDEExecute Chan, "[BARHEIGHT=0.25]" '
Set height to quarter inch
' generate the bar code
DDEExecute Chan, "[barcode=" + BCData$ + "]" '
send to B-Coder
'Save the barcode to disk
DDEExecute Chan, "[SAVEBARCODE(C:\barcode.wmf)]"
DDETerminate Chan ' Terminate the DDE link
'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
Quit:
End Sub
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).
|
 |