TextRange.InsertBarcode method (Publisher)
Inserts a bar code field at the end of the text range represented by the parent TextRange object.
Syntax
expression.InsertBarcode
expression A variable that represents a TextRange object.
Return value
TextRange
Remarks
Ideally, you should create an add-in to Microsoft Publisher to handle the MailMergeGenerateBarcode and MailMergeInsertBarcode events. If your add-in or code does not contain handlers for these events, the InsertBarcode method returns an error.
The example that follows shows how to handle these events by using Microsoft Visual Basic for Applications (VBA) code in the Visual Basic Editor.
If you want to enable insertion of bar codes into the publication from the user interface, your add-in or VBA code should also set the InsertBarcodeVisible property value to True.
Example
The following example shows how to use the InsertBarcode method to insert a bar-code field into a text box in a publication. Insert this code into your VBA project, and run the AttachToEvents procedure before running the InsertBarcode_Example procedure.
Before running the code in this example, use the MailMerge.OpenDataSource method to connect to a data source. The data source must contain a bar-code column that lists bar codes for all mail-merge recipients. Replace barcodeColumnIndex in the MailMergeGenerateBarcode event handler in the code with the index number of the data-source column that contains bar-code information.
Run the following code from the Visual Basic Editor window, and not from the Macros dialog box (on the Tools menu, point to Macro, and then choose Macros).
Public WithEvents pubApplication As Publisher.Application
Private Sub pubApplication_MailMergeGenerateBarcode(ByVal Doc As Document, bstrString As String)
bstrString = pubApplication.ActiveDocument.MailMerge.DataSource.DataFields.Item(barcodeColumnIndex).Value
End Sub
Private Sub pubApplication_MailMergeInsertBarcode(ByVal Doc As Document, OkToInsert As Boolean)
OkToInsert = True
End Sub
Public Sub InsertBarcode_Example()
Dim pubTextRange As Publisher.TextRange
Dim pubShape As Publisher.Shape
Set pubShape = ThisDocument.Pages(1).Shapes.AddTextbox(pbTextOrientationHorizontal, 100, 100, 500, 500)
Set pubTextRange = pubShape.TextFrame.TextRange
pubTextRange.InsertBarcode
End Sub
Public Sub AttachToEvents()
Set pubApplication = Application
End Sub
Support and feedback
Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.