'设置文档变量,并更新域。 '手动添加域的方法: '1、新建页眉页脚 '2、在页脚中插入域 “DOCVARIABLE 文档变量名” '3、在文件菜单,打印机设置菜单项中选定打印机 '4、运行宏“生成份数编号并打印到当前打印机” Function SetPrintCount(ByVal CountNumber As String) Dim i As Long Dim v As Variable 'Dim strVarName As String Const strVarName As String = "PrintCount" Dim strVarValue As String 'strVarName = "PrintCount" strVarValue = CountNumber Set v = ActiveDocument.Variables(strVarName) If v Is Nothing Then Set v = ActiveDocument.Variables.Add(strVarName, strVarValue) Else v.Value = strVarValue End If For Each v In ActiveDocument.Variables Debug.Print "variables", v.Name, v.Value Next Dim f As Field For Each f In ActiveDocument.Fields Debug.Print Left(f.Code.Text, Len(" DOCVARIABLE " & strVarName)) If f.Kind = wdFieldKindWarm And Left(f.Code.Text, Len(" DOCVARIABLE " & strVarName)) = " DOCVARIABLE " & strVarName Then '保留格式: \* MERGEFORMAT 'Debug.Print f.Code.Text f.Update End If Next End Function Sub 生成份数编号并打印到当前打印机() Dim i As Long Dim lngStart Dim lngCount lngCount = InputBox("请输入您要打印的份数", "请输入您要打印的份数", 1) If lngCount = "" Then Exit Sub End If lngStart = InputBox("请输入您要打印的起始编号", "请输入您要打印的起始编号", 1) If lngStart = "" Then Exit Sub End If For i = lngStart To lngCount SetPrintCount Format(i, "000000") '由于测试系统中没有打印机,所以这里暂时输出到打印文件。如果要直接打印,可以直接用 Application.PrintOut 即可。 Application.PrintOut , , , "c:\ayyyt" & i & ".prn", , , , , , , True, , "" Next End Sub |