views:

62

answers:

2

Hello.

This is the code that i have to export data to Excel.

Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object

oExcel = CreateObject("Excel.Application")
oBook = oExcel.Workbooks.Add

oSheet = oBook.Worksheets(1)
oSheet.Range("A1").Value = "ID"
oSheet.Range("B1").Value = " Nome"
oSheet.Range("A1:B1").Font.Bold = True
oSheet.Range("A2").Value = CStr(Request("ID"))
oSheet.Range("B2").Value = "John"

    oBook.SaveAs("C:\Book1.xlsx")
    oExcel.Quit()

I can create and save the excel file, but i can't update the contents. How can i do it?

Thanks.

A: 

You're trying to set a Range to a value, I think either you need to set a Range to an array that can contain the value(s) or you need to set a single Cell to a single value.

This link shows how to do either:

http://support.microsoft.com/kb/301982

ho1
But, how do i add more lines to the excel file, without being asked if i want the overwrite the contents of it?For each time, i just want to add one line to the file.Don't i have to use something like "If(File.exists) bla bla" ?
Filipe Costa
@Filipe Costa: I might have misunderstood your question. If you just want to be able to save the file without getting the prompts, before doing the save do `oExcel.DisplayAlerts = False` and then reset it to `True` after the save.
ho1
A: 

I think you want:

 oBook = oExcel.Workbooks.Open ("C:\Book1.xlsx")

When you choose Add, you are creating a new workbook.

If you are confident there are no gaps, something like this may suit:

''Last cell in column A, or first gap
oSheet.Range("a1").End(xlDown).Select
''A+1 row
oSheet.ActiveCell.Offset(1) = "a"
''A + 1 row + 1 col
oSheet.ActiveCell.Offset(1, 1) = "b"
''A + 1 row + 2 col
oSheet.ActiveCell.Offset(1, 2) = "c"

Otherwise, you may need http://support.microsoft.com/kb/142526 to determine the last cell.

Remou
Just opening the workbook doesn't mean that i can add a new line to it, right? How do i add a new line to the workbook, not overwriting what i have already?This is like one user submits a form with some data, that data goes to one line of the workbook. Another user submits the form, another line is added, and so on..
Filipe Costa
I have added a note
Remou