Insert Bar Codes into the Header or Footer of your Document
Background:
This article describes how to use the ActveX Control 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 10 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
- Create a User Form by going to the Insert Menu and chooseing User
Form.
- Rename the User Form "frmMain" using the Properties sheet
(View Propeties)
- Add a textbox to the Userform and name it "txtMessage"
- Add the TALBARCD control to the form.
- Add a command button and use the following code for its onClick Event:
frmMain.hide
- 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 (activeX Version)
' Macro written 6/28/01
'
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
'display form
frmMain.Show
'Get message to encode
BCData$ = frmMain.txtMessage.Text
' 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
frmMain.TALBarCd1.BarHeight = 250 'set height to 1/4"
frmMain.TALBarCd1.Message = BCData$ ' Generate the bar code
frmMain.TALBarCd1.SaveBarCode "C:\barcode.wmf" ' save to disk
'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).
|