在 Word 邮件合并中使用条形码
Word 365, 2024, 2021, 2019, 2016, 2013, 2010, 2007, 2003
1
打开现有邮件合并文档或创建新文档。
2
将条形码对象添加到邮件合并文档中。将其放置在文档中需要的位置,并设置对象的属性,如条形码类型和大小。
例如,看起来就像下面的截图。请注意条形码对象的名称(此处用黄色突出显示)。通常是 Barcode1。
3
邮件合并需要一个宏。您可以添加以下宏。将其复制到剪贴板,然后粘贴到 Word 的 Visual Basic for Applications 编辑器中:
Sub MailMerge_example_with_ActiveBarcode() ' Macro shows how to print barcodes in a mail merge. if MsgBox("Do you want to print mail merged documents?", vbYesNo, "Question") = vbYes Then 'counter to zero num = 0 'first record is number one ActiveDocument.MailMerge.DataSource.ActiveRecord = 1 Do ' Fill ActiveBarcode text property now ActiveDocument.Barcode1.Text = ActiveDocument.MailMerge.DataSource.DataFields("Productcode").Value ' do not prompt for printing... Options.PrintBackground = True ' print page ActiveDocument.PrintOut ' recognize old record lastone = ActiveDocument.MailMerge.DataSource.ActiveRecord ' select next record ActiveDocument.MailMerge.DataSource.ActiveRecord = wdNextRecord 'count num = num + 1 ' check if there are new ones Loop While ActiveDocument.MailMerge.DataSource.ActiveRecord <> lastone ' notify user MsgBox (Str(num) + " pages printed!") End If End Sub
4
结果如下所示。在截图中,你还可以看到插入宏的位置。
5
现在将宏调整为邮件合并文档:
如果您的条形码对象名称不是 Barcode1,请在宏中更改 - 屏幕截图中左侧的黄色标记。
现在,您可以更改数据源中的字段名,该字段名将用于条形码。本例中使用的是 Productcode - 屏幕截图中右侧的黄色标记。更改引号内的名称。
6
保存文档并启动宏进行测试打印。
提示: 开始测试打印时打印少量数据,以便在出错时减少错印。
7
完成。 如果您将宏放在 Normal.dot 中,而不是文档本身,则执行宏时文档必须不处于设计模式。否则将导致错误。因此,请在运行宏之前关闭设计模式。