G
gtslabs
I am trying to open an excel file and insert the data from a Table or
Query into a sheet and print.
I actually need to loop thru all the filtered records and do this
task.
Currently I am using this code to open the excel file and place the
data into Cell B2 then print. This part works but I need to integrate
the table/query loop to print all the records.
I have a table called tbl_data with 3 fields.
Data, ExcelFileName, ExcelSheetName
Private Sub PrintToExcel()
Dim objXLApp As Excel.Application
Dim objXLBook As Excel.Workbook
Dim objDataSheet As Excel.Worksheet
Set objXLBook = GetObject("C:\test.xls") ' Put File Name Here
'Set objXLBook = GetObject(Application.CurrentProject.Path &
"\ExcelFile\FileName.xls")
Set objXLApp = objXLBook.Parent
Set objDataSheet = objXLBook.Worksheets("Data") 'Select Worksheet
'Need to loop thru records and place field contents here.
objDataSheet.Cells(1, 2).Value = "data" ' test with string works.
objDataSheet.PrintOut Copies:=1, Collate:=True
objXLBook.Close savechanges:=False 'close without changes
Set objDataSheet = Nothing
Set objXLBook = Nothing
Set objXLApp = Nothing
End Sub
Query into a sheet and print.
I actually need to loop thru all the filtered records and do this
task.
Currently I am using this code to open the excel file and place the
data into Cell B2 then print. This part works but I need to integrate
the table/query loop to print all the records.
I have a table called tbl_data with 3 fields.
Data, ExcelFileName, ExcelSheetName
Private Sub PrintToExcel()
Dim objXLApp As Excel.Application
Dim objXLBook As Excel.Workbook
Dim objDataSheet As Excel.Worksheet
Set objXLBook = GetObject("C:\test.xls") ' Put File Name Here
'Set objXLBook = GetObject(Application.CurrentProject.Path &
"\ExcelFile\FileName.xls")
Set objXLApp = objXLBook.Parent
Set objDataSheet = objXLBook.Worksheets("Data") 'Select Worksheet
'Need to loop thru records and place field contents here.
objDataSheet.Cells(1, 2).Value = "data" ' test with string works.
objDataSheet.PrintOut Copies:=1, Collate:=True
objXLBook.Close savechanges:=False 'close without changes
Set objDataSheet = Nothing
Set objXLBook = Nothing
Set objXLApp = Nothing
End Sub