
Using Excel workbooks from VB
I assume you are doing this in VB and not
VBA (from within Excel).
Accordingly, you need to do a couple of things. First off, set a
reference to the Excel Object Library in VB. Your code needs to create
an instance of Excel for you to do what you are looking for. However,
your instance can remain invisible to the user. Here is an example
that will:
1) Create an instance of Excel
2) Add a new workbook
3) Add a new worksheet to the book
4) Add the word "hello" to cell A1
5) Save the workbook
6) Close Excel
-Dan Beacom
Option Explicit
Private Sub Form_Load()
Dim ExcelApp As Excel.Application
Dim NewWorkbook As Excel.Workbook
Dim NewWorksheet As Excel.Worksheet
Set ExcelApp = CreateObject("Excel.Application.8")
Set NewWorkbook = ExcelApp.Workbooks.Add
Set NewWorksheet = NewWorkbook.Worksheets.Add
NewWorksheet.Cells(1, 1).Value = "hello"
ExcelApp.Visible = False
NewWorkbook.SaveAs ("C:\My Documents\Hello.xls")
ExcelApp.Quit
Set ExcelApp = Nothing
Set NewWorkbook = Nothing
Set NewWorksheet = Nothing
Unload Me
End Sub
On Tue, 12 Jan 1999 21:40:13 -0000, "Big Cheese"
Quote:
>I want to get a list of the Worksheets within a Workbook. I have the
>following piece of code to do this:
> Dim xlws As Excel.Worksheet
> Dim xl as Excel.Workbook
> For Each xlws In xl.Worksheets
> Combo1.AddItem xlws.Name, count
> count = count + 1
> Next xlws
>Now, how do I Set xl to the Workbook?
>I want to use a .xls file directly, i.e. Excel shouldn't have to be running.
>Thanks,
>Geoff