Working from Access VBA your code should look something like this.
Please note where some actions are done by Apxl
object, while others must be done by the object's Application
property. I have found this issue vital in various Excel actions done from MS Access.
First create an Excel objet:
dim Apxl as Object, xlWorkbook as Object ,xlWorksheet as Object
Set Apxl = CreateObject("Excel.Application")
Next If the Workbook
(the file) exists already, open it:
ApXl.Workbooks.Open("C\:MyPath\MyFileName.xlsx")
Or create a new one:
Set xlWorkbook = Apxl.Application.WorkBooks.Add
xlWorkbook.SaveAs "C\:MyPath\MyFileName.xlsx"
Now you can approach the Worksheat
Object by it's name, or ordinal number (startin by 1 ):
Set xlWorksheet = xlWorkbook.Worksheets("MySheetName")
Set xlWorksheet = xlWorkbook.Worksheets(1)
If sheet does not exist, you can create a new one:
xlWorkbook.Worksheets.Add
The new worksheet will be added to the beginning of the collection.
You can also rename the WorkSheet
:
Set xlWorksheet = xlWorkbook.Worksheets(1)
xlWorksheet.Name = "MyNewName"