下面发布的代码会创建一个包含for循环的每次迭代的宏的工作簿的副本。从Excel复制到Word并打印,代码会在每次打印时创建工作簿的副本
该代码将一些信息从一张纸传输到名为“Ticket”的纸张。代码然后打开一个Word文件,其中包含公司徽标的页眉和页脚以及水印,将信息从Excel工作表(“工单”)复制到带有水印的Word文档中,然后打印Word文档。一旦代码执行完毕,就会为每张打印的票证,Book1,Book2,Book3等(全部隐藏)提供一本新的Excel书籍(隐藏书籍)。我不知道这些书籍在哪里被保存或如何阻止这种情况发生。
有人可以解释我做了什么吗?
Sub A_PrintDailyTickets()
'---------------------------------------------------------------------------------------
' Procedure : A_PrintDailyTickets
' Author : AWS
' Date : 9/5/2015
' Purpose : Print a full day's worth of tickets for all three trucks, with word using the Soul's Harbor water mark
' Complete 9/5/2015
'
'---------------------------------------------------------------------------------------
Dim lLstRow As Long, ws As Worksheet
Dim WdObj As Object, fname As String ' , objDoc As Object
Application.ScreenUpdating = False
Set ws = ActiveSheet
fname = "Word"
With Sheets("Ticket")
lLstRow = ActiveSheet.Range("A50").End(xlUp).Row
For i = 2 To lLstRow
Sheets("Ticket").Cells(2, 4).Value = ws.Cells(i, 1).Value ' Date
Sheets("Ticket").Cells(4, 3).Value = ws.Cells(i, 2).Value ' Route
Sheets("Ticket").Cells(6, 8).Value = ws.Cells(i, 4).Value ' Phone-1
Sheets("Ticket").Cells(7, 8).Value = ws.Cells(i, 5).Value ' Phone-2
Sheets("Ticket").Cells(6, 3).Value = ws.Cells(i, 6).Value ' Name
Sheets("Ticket").Cells(7, 3).Value = ws.Cells(i, 7).Value ' Address
Sheets("Ticket").Cells(8, 3).Value = ws.Cells(i, 8).Value & ", TX" ' City
Sheets("Ticket").Cells(9, 5).Value = ws.Cells(i, 9).Value ' Zip
Sheets("Ticket").Cells(14, 3).Value = ws.Cells(i, 10).Value ' Items
Sheets("Ticket").Cells(21, 3).Value = ws.Cells(i, 11).Value ' Notes
Set WdObj = CreateObject("Word.Application")
WdObj.Visible = False
Sheets("Ticket").Select
Range("A1:H30").Select
Selection.Copy 'Your Copy Range
WdObj.Documents.Open Filename:= _
"C:\Users\AWS\Documents\Excel\Zip Codes - Soul's Harbor\Monthly Route Sheets\Donor Receipt\Soul's Harbor Donation Templet (Blank) - Usable - 2.docx"
WdObj.Selection.PasteSpecial Link:=False, _
DataType:=wdPasteText, Placement:=wdInLine, DisplayAsIcon:=False
Application.CutCopyMode = False
If fname <> "" Then 'make sure fname is not blank
With WdObj
'.ChangeFileOpenDirectory "c:\temp" 'save Dir
'.ActiveDocument.SaveAs Filename:=fname & ".doc"
End With
Else:
MsgBox ("File not saved, naming range was botched, guess again.")
End If
WdObj.PrintOut
WdObj.ActiveDocument.Close savechanges:=False
WdObj.Quit savechanges:=False
Range("C1:H30").Select
Selection.ClearContents
Range("E1").Select
Application.CutCopyMode = False
Set WdObj = Nothing
'Set objDoc = Nothing
Next
End With
ws.Select
Set ws = Nothing
Set WdObj = Nothing
'Set objDoc = Nothing
Application.ScreenUpdating = True
End Sub
我在代码中看到很多问题,但并不完全在生成工作表的地方。我将从“With Sheets”(“Ticket”)开始,说明您没有使用期限来限定房产。例如在Range(“C1:H30”)上选择''应该是'.Range(“C1:H30”)。选择'与“Ticket”表单中的范围相关。修复该参考,以便它更清晰,并且您的问题将更容易识别。我建议你在循环开始时设置一个断点,然后按下F8逐步浏览并查看每个步骤的创建方式。此致, – nbayly
另请参阅[如何避免使用选择Excel VBA宏](http://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba-macros) – aucuparia