下面發佈的代碼會創建一個包含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