可以打印“份数”的Word宏
前两天做上市会,打印试乘试驾保证书,想自动打印份数编号。不会 VBA,幸亏群里“江山”指点迷津,并亲自写出代码,特纪念在此
1 Sub PRNTbyZSM() 2 3 Dim fsbh As String '份数编号 4 Dim l As Integer 5 Dim i As Integer 6 Dim j As Integer 7 8 Dim c As Integer 9 10 c = Val(InputBox("请输入总份数", "输入份数,默认10份", 10)) 11 12 Selection.TypeBackspace 13 14 Windows("奔腾试乘试驾保证书").Activate 15 Selection.WholeStory 16 Selection.Copy 17 Documents.Add DocumentType:=wdNewBlankDocument 18 Selection.PasteAndFormat (wdPasteDefault) 19 ActiveWindow.ActivePane.VerticalPercentScrolled = 49 20 If MsgBox("即将打印" & c & "份,请再次确认!", vbQuestion + vbYesNo) = vbYes Then 21 For i = 1 To c 22 Selection.HomeKey Unit:=wdStory 23 Selection.EndKey Unit:=wdLine 24 fsbh = Format(i, "00000") 25 l = Len(fsbh) 26 Selection.TypeText Text:=fsbh 27 ActiveDocument.PrintOut 28 For j = 1 To l 29 Selection.TypeBackspace 30 Next j 31 DoEvents 32 Next i 33 MsgBox "打印文档已经发送到打印机!" 34 ActiveWindow.Close 35 End If 36 End Sub