The code posted below creates a copy of the workbook containing the macro each iteration of the for loop.
The code transfers some information from one sheet to a sheet named "Ticket". The code then opens a Word file which has a header and footer and a watermark of the company logo, copies the info from the Excel sheet ("Ticket") to the Word document with the watermark, and then prints the Word document. Once the code has finished executing there is a new Excel book (a hidden book at that) for each ticket printed, Book1, Book2, Book3, etc (all hidden). I have no idea where these books are being saved or how to stop this from happening.
Can someone explain what I've done, please?
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