Is it possible to launch excel from VB and execute a macro ?
Printable View
Is it possible to launch excel from VB and execute a macro ?
Select Microsoft Excel 8.0 Object Library in the Project references.
Do something like...
Dim objExcel As Excel.Application
Dim objBook As Excel.Workbook
Set objExcel = New Excel.Application
Set objBook = objExcel.Workbooks.Add
objExcel.Visible = True
objBook.Sheets(1).Cells(1,1).Value = "HELLO"
objBook.Sheets(1).Cells(1,1).Font.Bold = True
Etc...
Remember to Quit Excel after use and set objects to nothing to avoid sessions being left open in the background as VB gets upset. Not sure about executing a pre-written macro as I've always programmed what I need to do in VB.
PS I have a question.
Has anybody tried to close down an Excel application when a chart object has been created in a workbook which has been saved and closed.
I found out that the chart object keeps a link to the cell range that is the chart source data. This prevents Excel from closing properly.
The way I got round this was to use GetObject and a loop, and close any applications that were lingering - any other ideas??
That certainly shows you how to openexcel. As for running Excel Macros, try:
Code:Dim xls As New Excel.Application
Dim wrk As Excel.Workbook
Set wrk = xls.Workbooks.Open("c:\MySheet.xls")
xls.Run "MyMacro"