'設置文檔變量,并更新域。 '手動添加域的方法︰ '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 |