TextRange.InsertBarcode 方法 (Publisher)

由父 TextRange 对象代表文本范围的末尾插入条码域。

语法

表达式。InsertBarcode

表达式 表示 TextRange 对象的变量。

返回值

TextRange

备注

理想情况下,您应该创建外接程序中向 Microsoft Publisher 处理 MailMergeGenerateBarcode 和 MailMergeInsertBarcode 事件。 如果您在加载或代码不包含这些事件处理程序, InsertBarcode 方法将返回错误。

下面的示例演示了如何使用 Visual Basic 编辑器中的 Microsoft Visual Basic for Applications (VBA) 代码来处理这些事件。

如果您想要启用从用户界面的出版物条码插入,您加载项或 VBA 代码还应设置 InsertBarcodeVisible 属性值为 True 。

示例

下面的示例演示如何使用 InsertBarcode 方法将条码字段插入到出版物中的文本框。 这段代码插入 VBA 项目,和 AttachToEvents 过程运行之前运行的 InsertBarcode_Example 过程。

运行代码之前在此示例中,使用 MailMerge.OpenDataSource 方法来连接到数据源。 数据源必须包含所有邮件合并收件人都列表条码条码列。 将代码中 MailMergeGenerateBarcode 事件处理程序中的 barcodeColumnIndex 替换为包含条形码信息的数据源列的索引号。

从 Visual Basic 编辑器窗口运行以下代码,而不是从“工具”菜单上的“宏”对话框 (运行,指向“宏”,然后选择“宏) ”。

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

支持和反馈

有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。