Menu

PDF417 in Microsoft Office Automation

Using PDF417-ActiveX in MS Office Automation

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

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





How to create a Word document and insert a PDF417 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 PDF417-ActiveX")
app.Selection.TypeParagraph
app.Selection.TypeParagraph

'create the PDF417-ActiveX
Set oShape = app.Selection.InlineShapes.AddOLEObject("PDF417ActiveX.PDF417Ctrl.1")

'get the created object and set properties
oShape.Width = 260
oShape.Height = 150
Set oPDF417 = oShape.OLEFormat.Object
oPDF417.DataToEncode = "1234567"
oPDF417.CompactionMode = 0

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.


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

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

Const BarcodeMarker = "BarcodePlace"
Const TmpFileName = "c:\P83BF5BA6020.gif"
Dim WithEvents wdapp As Application
Dim barcode As PDF417Ctrl
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("PDF417ActiveX.PDF417Ctrl.1")
barcode.CompactionMode = cmAuto
'barcode.BackColor = RGB(255, 0, 0)
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.DataToEncode = Doc.MailMerge.DataSource.DataFields(DataFieldIndex).Value
Dim w, h
Call barcode.GetPDF417Size(BarcodeModule, 1, 1, dmPixels, dmPixels, w, h)
Call barcode.SaveToImageFile(w, h, 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