E
Ernesto
hello all,
I know the subject line is long, but i wanted to be as descriptive as
i could possibly be. if you dont want to read my story skip to the
next paragraph. here was my story, i was looking for a piece of code
which could automatically extract individual lines/rows/records from a
query to Excel. But not just any Excel file, but a template. so after
days of searching and combining different codes here is what I came up
with.
Here is what the code does: using "qryTransferToPM", each row is
extracted to the following sheet Planning Data" in the following
workbook "Accessory Price Worksheet Templatev4_multiplerows.xls". It
then saves each workbook as "Product 1", "Product 2", "Product 3",
etc. It saves the files in the same location as the database.
by no means am I an expert or good at this. I created this code out of
necessity. so if you find a better way to write the below
code...please let me know.
Here is the code. Please provide comments/updates:
======================================================
Function GetPath(Filename As String) As String
GetPath = (Mid(Filename, 1, Len(Filename) - Len(Dir(Filename))))
End Function
======================================================
Option Compare Database
Sub querytoexcel()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim fld As DAO.Field
Dim fld2 As DAO.Field
Dim objXL As Excel.Application
Dim objXLBook As Excel.Workbook
Dim objWS As Excel.Worksheet
Dim intCol As Integer
Dim intRow As Integer
Dim intName As Integer
Set db = CurrentDb
'get data into recordset
Set rs = db.OpenRecordset("qryTransferToPM")
conPath = GetPath(db.Name)
'rs2 = rs
'launch excel
Set objXL = New Excel.Application
'Naming varible
intName = 0
Do Until rs.EOF
'create worksheet
Set objXLBook = objXL.Workbooks.Open(conPath & "Accessory Price
Worksheet Templatev4_multiplerows.xls")
'Set objWS = objXL.Sheets("qryTheQuery")
Set objWS = objXL.Sheets("Planning Data")
'copy data
'first field names
For intCol = 0 To rs.Fields.Count - 1
Set fld = rs.Fields(intCol)
objWS.Cells(1, intCol + 1) = fld.Name
Next intCol
intName = intName + 1
'then actual data
intRow = 2
'Do Until rs.EOF
For intCol = 0 To rs.Fields.Count - 1
objWS.Cells(intRow, intCol + 1) = _
rs.Fields(intCol).Value
Next intCol
rs.MoveNext
intRow = intRow + 1
'objXL.Sheets("Planning Data").Visible = False
objXLBook.SaveAs (conPath & "Product_" & intName & ".xls")
objXLBook.Close
Loop
'objXLBook.Close
End Sub
======================================================
I know the subject line is long, but i wanted to be as descriptive as
i could possibly be. if you dont want to read my story skip to the
next paragraph. here was my story, i was looking for a piece of code
which could automatically extract individual lines/rows/records from a
query to Excel. But not just any Excel file, but a template. so after
days of searching and combining different codes here is what I came up
with.
Here is what the code does: using "qryTransferToPM", each row is
extracted to the following sheet Planning Data" in the following
workbook "Accessory Price Worksheet Templatev4_multiplerows.xls". It
then saves each workbook as "Product 1", "Product 2", "Product 3",
etc. It saves the files in the same location as the database.
by no means am I an expert or good at this. I created this code out of
necessity. so if you find a better way to write the below
code...please let me know.
Here is the code. Please provide comments/updates:
======================================================
Function GetPath(Filename As String) As String
GetPath = (Mid(Filename, 1, Len(Filename) - Len(Dir(Filename))))
End Function
======================================================
Option Compare Database
Sub querytoexcel()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim fld As DAO.Field
Dim fld2 As DAO.Field
Dim objXL As Excel.Application
Dim objXLBook As Excel.Workbook
Dim objWS As Excel.Worksheet
Dim intCol As Integer
Dim intRow As Integer
Dim intName As Integer
Set db = CurrentDb
'get data into recordset
Set rs = db.OpenRecordset("qryTransferToPM")
conPath = GetPath(db.Name)
'rs2 = rs
'launch excel
Set objXL = New Excel.Application
'Naming varible
intName = 0
Do Until rs.EOF
'create worksheet
Set objXLBook = objXL.Workbooks.Open(conPath & "Accessory Price
Worksheet Templatev4_multiplerows.xls")
'Set objWS = objXL.Sheets("qryTheQuery")
Set objWS = objXL.Sheets("Planning Data")
'copy data
'first field names
For intCol = 0 To rs.Fields.Count - 1
Set fld = rs.Fields(intCol)
objWS.Cells(1, intCol + 1) = fld.Name
Next intCol
intName = intName + 1
'then actual data
intRow = 2
'Do Until rs.EOF
For intCol = 0 To rs.Fields.Count - 1
objWS.Cells(intRow, intCol + 1) = _
rs.Fields(intCol).Value
Next intCol
rs.MoveNext
intRow = intRow + 1
'objXL.Sheets("Planning Data").Visible = False
objXLBook.SaveAs (conPath & "Product_" & intName & ".xls")
objXLBook.Close
Loop
'objXLBook.Close
End Sub
======================================================