Menu

Using Barcodes in MS Automation

Using barcodes in MS Office Automation

How to create a Word document and insert a barcode into it?

How to insert a barcode into MS Excel sheet?

Is there any way to use a Barcode ActiveX in Word with a mail merge field and how would this be done?





How to create a Word document and insert a barcode into it?

Below you can see an example written in VB Script that starts the Word application and inserts a barcode into the document.
set app = CreateObject("Word.Application")

Set doc = app.Documents.Add
doc.Activate

app.Selection.Font.Size = 32
app.Selection.Font.Bold = True
app.Selection.TypeText("Hello ActiveX")
app.Selection.TypeParagraph
app.Selection.TypeParagraph

'create the ActiveX
Set oShape = app.Selection.InlineShapes.AddOLEObject("ABarCode.ActiveBC.1")

'get the created object and set properties
oShape.Width = 190
oShape.Height = 70
Set oBarCode = oShape.OLEFormat.Object
oBarCode.BarType = 13 'UPC-A
oBarCode.BarText = "89981294090"
oBarCode.ShowCheck = True
oBarCode.TextAlign = 5 'EAN2

set f = CreateObject("StdFont")
f.Name = "Courier"
f.Size = 25
oBarCode.Font = f

doc.SaveAs "C:\test.doc"

doc.Close
app.Quit

set doc = nothing
set app = nothing

MsgBox "Ok"
You can copy this text to Notepad and save it with the .vbs extension and then start this .vbs file. This will create the file test.doc on disk "C" with a barcode in it.

Besides, you can use it from VBA (Visual Basic for Applications), i.e. from Excel, Word, Access, etc.




How to insert a barcode into MS Excel sheet?

Below you can see an example written in VBA (Visual Basic for Applications).
Public Sub MyScript()

'create barcode object
If ActiveSheet.OLEObjects.Count = 0 Then
  Set oleObj = ActiveSheet.OLEObjects.Add(ClassType:="ABarCode.ActiveBC.1", _
    Left:=10, _
    Top:=10, _
    Width:=190, _
    Height:=70)
Else
  Set oleObj = ActiveSheet.OLEObjects(1)
End If

barcodecell = Sheets(1).Range("B2").Value

Set oBarCode = oleObj.Object
oBarCode.BarType = 13 'UPCA
'oBarCode.BarText = "123456789012"
'oBarCode.BarText = barcodecell
oBarCode.TextAlign = 4

oleObj.LinkedCell = "$B$5"

Set f = CreateObject("StdFont")
f.Name = "Courier"
f.Size = 25
oBarCode.Font = f

End Sub


Is there any way to use a Barcode ActiveX in Word with a mail merge field and how would this be done?

You can use our Barcode ActiveX in a mail-merge document. You should use macros for that.
Const DataFieldIndex = 4
Const BarcodeModule = 3

Const BarcodeMarker = "BarcodePlace"
Const TmpFileName = "c:\A83BF5BA6020.gif"
Dim WithEvents wdapp As Application
Dim barcode As ActiveBC
Dim ishapeNew As InlineShape
Dim bcRange As Range
Dim bcFound As Boolean

Private Sub Document_Open()
'Get a reference to the Word application to handle mail merge events.
Set wdapp = Application

'create the barcode object and set up properties
Set barcode = CreateObject("ABarCode.ActiveBC.1")
barcode.BarType = Code39
barcode.CalcCheck = False
End Sub

Private Sub Document_Close()
Set wdapp = Nothing
End Sub

Private Sub wdapp_MailMergeDataSourceLoad(ByVal Doc As Document)
End Sub

Private Sub wdapp_MailMergeBeforeRecordMerge(ByVal Doc As Document, Cancel As Boolean)
Set bcRange = Doc.Content
bcRange.Find.Execute FindText:=BarcodeMarker, MatchWholeWord:=True
bcFound = bcRange.Find.Found

If bcFound = True Then
'saves a barcode picture
barcode.BarText = Doc.MailMerge.DataSource.DataFields(DataFieldIndex).Value
Dim w
w = barcode.GetBarcodeWidth(BarcodeModule, 1, 1, dmPixels)
Call barcode.SaveToImageFile(w, 100, TmpFileName)

'adds a barcode picture to the Word document
Set ishapeNew = Doc.InlineShapes.AddPicture(TmpFileName, False, True, bcRange)

'deletes the barcode marker
bcRange.Start = bcRange.Start + 1
bcRange.Delete
End If
End Sub

Private Sub wdapp_MailMergeAfterRecordMerge(ByVal Doc As Document)
If bcFound = True Then
'adds the barcode marker
bcRange.InsertBefore (BarcodeMarker)

'delete the barcode picture
ishapeNew.Delete
End If
End Sub

Private Sub wdapp_MailMergeAfterMerge(ByVal Doc As Document, ByVal DocResult As Document)
Kill (TmpFileName)
End Sub

Here is you can download the source files.
Download

First, you should create a data source that will be used for mail merge. We use an Access database in our example, but it can be any other data source (refer to MS Word documentation).

Create a data source that will be used for mail merge

Start MS Word and show the Mail Merge toolbox.

Show mail merge toolbox.

Open Data Source.

Open Data Source.

Insert Merge Fields.

Insert Merge Fields.

Type a "barcode marker". The phrase "BarcodePlace" is a barcode marker. The macro that will be created later will insert a barcode instead of this phrase.

Type a barcode marker

Change Security Level to Low or set Trusted Sources and start Visual Basic Editor (Alt+F11).
Add "Barcode Type Library" to the project references.
Copy and paste the text of the macro.

Copy and paste the text of the barcode macro

Save the created Word document and close it. Then open it again.
You can print the mail-merge document or save it to another Word document.

Barcode merge result