Article ID: 129304
Article Last Modified on 1/19/2007
Dim mysheet As Object, myfield As Variant
' Set object variable equal to the OLE object.
Set mysheet = GetObject("c:\access2\ole_test.xls", "excel.sheet")
' Put the value of the ToExcel text box into the cell on the
' spreadsheet and make the cell bold.
myfield = Me!ToExcel
mysheet.cells(1, 1).value = myfield
mysheet.cells(1, 1).font.bold = True
' Set the Visible property of the sheet to True, save the
' sheet, and quit Microsoft Excel.
mysheet.application.windows("ole_test.xls").visible = True
mysheet.application.activeworkbook.save
mysheet.application.activeworkbook.[close]
mysheet.application.[quit]
' Clear the object variable.
Set mysheet = Nothing
NOTE: The brackets around the words "close" and "quit" are necessary
to specify that these are Microsoft Excel methods, rather than a
Microsoft Access methods.
Dim mysheet As Object, myfield As Variant
' Set object variable equal to the OLE object.
Set mysheet = GetObject("c:\access2\ole_test.xls", "excel.sheet")
' Put the value of the ToExcel text box into the cell on the
' spreadsheet and make the cell bold.
myfield = Me!ToExcel
mysheet.cells(1, 1).Value = myfield
mysheet.cells(1, 1).font.bold = True
' Set the Visible property of the sheet to True, save the
' sheet, and quit Microsoft Excel.
mysheet.Application.windows("ole_test.xls").Visible = True
mysheet.Application.activeworkbook.Save
mysheet.Application.activeworkbook.Close
mysheet.Application.Quit
' Clear the object variable.
Set mysheet = Nothing
In Microsoft Access 97:
Dim mysheet As Object, myfield As Variant, xlApp As Object
' Set object variable equal to the OLE object.
Set xlApp = CreateObject("Excel.Application")
' Set mysheet = GetObject("c:\access2\ole_test.xls", "excel.sheet").
Set mysheet = xlApp.workbooks.Open("c:\access2\ole_test.xls").Sheets(1)
' Put the value of the ToExcel text box into the cell on the
' spreadsheet and make the cell bold.
myfield = Me!ToExcel
mysheet.cells(1, 1).Value = myfield
mysheet.cells(1, 1).font.bold = True
' Set the Visible property of the sheet to True, save the
' sheet, and quit Microsoft Excel.
mysheet.Application.windows("ole_test.xls").Visible = True
mysheet.Application.activeworkbook.Save
mysheet.Application.activeworkbook.Close
xlApp.Quit
' Clear the object variable.
Set mysheet = Nothing
Private Sub Command0_Click()
Dim DB As Database, Rs As Recordset
Dim i As Integer, j As Integer
Dim RsSql As String
Dim CurrentValue As Variant
Dim CurrentField As Variant
Dim Sheet As Object
Set DB = DBEngine.Workspaces(0).Databases(0)
' *** Use the following line for Microsoft Access 7.0 only ***
' RsSql = "SELECT * FROM [Order Details] WHERE [OrderId]<10249;"
' *** Use the following line for Microsoft Access 2.0 only ***
' RsSql = "SELECT * FROM [Order Details] WHERE [Order Id]<10001;"
Set Rs = DB.OpenRecordset(RsSql, DB_OPEN_DYNASET)
Set Sheet = CreateObject("Excel.Sheet")
j = 1
' Loop through the Microsoft Access field names and create
' the Microsoft Excel labels.
For i = 0 To Rs.Fields.Count - 1
CurrentValue = Rs.Fields(i).Name
Sheet.cells(j, i + 1).Value = CurrentValue
Next i
j = 2
' Loop through the Microsoft Access records and copy the records
' to the Microsoft Excel spreadsheet.
Do Until Rs.EOF
For i = 0 To Rs.Fields.Count - 1
CurrentField = Rs(i)
Sheet.cells(j, i + 1).Value = CurrentField
Next i
Rs.MoveNext
j = j + 1
Loop
' Print the Microsoft Excel spreadsheet.
Sheet.PrintOut
Set Sheet = Nothing
End Sub
In Microsoft Access 97:
Private Sub Command0_Click()
Dim DB As Database, Rs As Recordset
Dim i As Integer, j As Integer
Dim RsSql As String
Dim CurrentValue As Variant
Dim CurrentField As Variant
Dim Workbook As Object
Dim xlApp As Object
Dim Sheet As Object
Set DB = DBEngine.Workspaces(0).Databases(0)
RsSql = "SELECT * FROM [Order Details] WHERE [OrderId]<10249;"
Set Rs = DB.OpenRecordset(RsSql, DB_OPEN_DYNASET)
Set xlApp = CreateObject("Excel.Application")
xlApp.workbooks.Add
Set Sheet = xlApp.activeworkbook.sheets(1)
j = 1
' Loop through the Microsoft Access field names and create
' the Microsoft Excel labels.
For i = 0 To Rs.Fields.Count - 1
CurrentValue = Rs.Fields(i).Name
Sheet.cells(j, i + 1).Value = CurrentValue
Next i
j = 2
' Loop through the Microsoft Access records and copy the records
' to the Microsoft Excel spreadsheet.
Do Until Rs.EOF
For i = 0 To Rs.Fields.Count - 1
CurrentField = Rs(i)
Sheet.cells(j, i + 1).Value = CurrentField
Next i
Rs.MoveNext
j = j + 1
Loop
' Print the Microsoft Excel spreadsheet.
Sheet.PrintOut
' Close workbook without saving.
xlApp.activeworkbook.saved = True
Set Sheet = Nothing
xlApp.Quit
Set xlApp = Nothing
End Sub
Option Explicit
Keywords: kbfaq kbinfo kbprogramming KB129304