Using Excel workbooks from VB 
Author Message
 Using Excel workbooks from VB

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



Sat, 30 Jun 2001 03:00:00 GMT  
 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



Sun, 01 Jul 2001 03:00:00 GMT  
 Using Excel workbooks from VB
Hi,

To clarify, this does actually start up an instance of Excel so you in fact
require Excel to be installed on your computer.  If you ctl-alt-del to bring
up task manager, you'll see that Excel is running (while your program is
running) although it won't be visible.  If Excel is not installed, you won't
have the Excel Object Library reference to select.

I have a question to add myself.  I have created an embedded Excel object in
a child form (to an MDIForm object) so that a user can modify and view an
Excel spreadsheet within my VB program (ie not have Excel running in a
separate window).  However, it seems that when this object is opened for
editing it sets the height and width itself and I cannot adjust this.  Does
anyone know of a way to do this either in regular code or through the API?
Also, is there a way to have Excel save the changes?  There is no file menu
that comes up and if I try something like
xlEmbedObj.Object.ActiveWorkbook.Save, I get a run-time error telling me
that the Save method is not valid for that object.

Thanks in advance.

Fernando Vicente

Quote:

>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




Mon, 02 Jul 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Excel HRESULT: 0x800A03EC when using Workbooks.open or Workbooks._opentext in C#

2. How can I open Excel workbook without using Excel.Worksheet

3. ASP using a VB DLL to open an Excel workbook

4. how delete a macro within an Excel Workbook by using VB

5. how delete a macro within an Excel Workbook by using VB

6. Excel 97/2000: making custom functions (in a hidden workbook) available to all open workbooks

7. VBA: Excel workbook to workbook transfer

8. combine multiple excel workbooks into one workbook

9. Using Excel workbooks and sheet from Access

10. Using VBA to Import a Range from an Excel Workbook

11. Open Excel workbook using ADO

12. Saving Excel Workbook in ASCII format using VBScript

 

 
Powered by phpBB® Forum Software