open and write an excel file

Eliot

New member
Local time
Today, 07:13
Joined
Dec 18, 2007
Messages
8
Hello all!

I'm having problems opening and writing into an excel file, the code i've got is the following:

Dim xlApps As New Excel.Application
Dim xlwrkbs As Excel.workbook
Dim xlsht As Excel.worksheet


Set xlwrkbs = GetObject("Text.xls")
Set xlsht = xlwrkbs.worksheet(1)

xlsht.cells(1, "A") = "testing"
xlwrkbs.Save

But I get an error in the first line, saying user-defined type not defined...

How could I create a new file? Anyone has an example??
Thanks in advance!
 
Try this:

Code:
Private Sub CopyToSpreadSheet()
    Dim objExcel As Excel.Application
    Dim objWB As Excel.Application
    Dim objWS As Excel.Worksheet
    Dim rst As DAO.Recordset


    Set rst = CurrentDb.OpenRecordset("YourQueryNameHere")


    Set objExcel = CreateObject("Excel.Application")
        objExcel.Visible = True
    Set objWB = objExcel.Workbooks.Open("YourPathAndFileNameHere")
    Set objWS = objWB.Worksheets("YourWorksheetNameHere")


        objWS.Range("G32").CopyFromRecordset rst

End Sub
 

Users who are viewing this thread

Back
Top Bottom