I have a continuous form and I want to be able to copy data from it like one would copy from a spreadsheet (or datasheet view in Access 2007), i.e. being able to select and copy any combination of rows and columns. If you can figure this out... Help is appreciated.
Make a reference to the Microsoft.Excel. Here's my spreadsheet code:
Public Sub PrintToXLS(strQryName As String, strTitle As String, intDataStartCol As Integer, Optional intDataStartRow = 2)
On Error GoTo err_trap
Dim xls As Excel.Application
Dim wb As Excel.Workbook
Dim myRange As Excel.Range
Dim i As Integer
Dim j As Integer
Dim intCols As Integer
Dim intRows As Integer
Dim nValue As single
Dim sFileName As String
sFileName = CurrentProject.Path & "\timesheet.xls"
If Dir$(sFileName) <> "" Then
Kill sFileName
End If
'Now run the x-tab query in Access and output to Excel 2003.
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, strQryName, sFileName
Set xls = New Excel.Application
Set wb = Workbooks.Open(sFileName)
Set myRange = ActiveSheet.UsedRange
'Get a range/count of cells that are used.
intCols = myRange.Columns.Count
intRows = myRange.Rows.Count
'Add up the cells vertically to get a sum amount.
For j = intDataStartCol To intCols
nValue = 0
For i = intDataStartRow To intRows
nValue = Cells(i, j).Value + nValue
Next 'Next cell
Cells(i, j).Value = nValue 'insert the sum now that we are done with the column.
Cells(i, j).Font.Bold = True 'bold it
Next j 'Next column
With Cells(intRows + 1, intDataStartCol - 1)
.Value = "Total"
.Font.Bold = True
.Select
.HorizontalAlignment = xlRight
End With
'Add up the totals horizontally to get a sum amount.
For i = intDataStartRow To intRows
nValue = 0
For j = intDataStartCol To intCols
nValue = Cells(i, j).Value + nValue
Next j 'Next column
Cells(i, j).Value = nValue 'insert the sum now that we are done with the column.
Cells(i, j).Font.Bold = True 'bold it
Next i 'Next row
With Cells(1, j)
.Value = "Total"
.Font.Bold = True
.Select
.HorizontalAlignment = xlRight
End With
'Resize the columns
For i = 1 To intCols
myRange.Columns(i).AutoFit
Next i
'Bold the headers/field names.
myRange.Rows(1).Font.Bold = True
'Change the font size for the column names and data.
myRange.Font.Size = 6
'Format some printing/page setup items.
With ActiveSheet.PageSetup
.LeftHeader = "&""Arial,Bold""&12" & strTitle 'This is our title.
.CenterHeader = ""
.RightHeader = ""
.LeftFooter = Date
.CenterFooter = ""
.RightFooter = "Page &P of &N"
.PrintGridlines = True
.Orientation = xlLandscape
.LeftMargin = Application.InchesToPoints(0.5)
.RightMargin = Application.InchesToPoints(0.5)
End With
'Refresh the spreadsheet since we've made changes.
wb.RefreshAll
'Print out the spreadhseet.
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
cleanup:
'Close the workbook and don't save changes.
If Not (wb Is Nothing) Then
wb.Close False
End If
If Not (wb Is Nothing) Then
Set wb = Nothing
End If
If Not (xls Is Nothing) Then
Set xls = Nothing
End If
Exit Sub
err_trap:
MsgBox Err.Number & ": " & Err.Description, vbCritical
Resume cleanup
I know you'll have to alter it in some way but take a look at it.
--Craig