View Single Post
  #3   Report Post  
Posted to comp.lang.basic.visual.misc,microsoft.public.excel.programming
AustinMN AustinMN is offline
external usenet poster
 
Posts: 2
Default How to create Excel workbook from VB6?

"Bob Phillips" wrote in message
...
Hi Kurt,

If you create an Excel instance, then you have access to all of the

objects.
so as an example

Dim xlApp As Object
Dim xlWB As Object
Dim xlWS As Object

Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True ' or not as required
Set xlWB = xlApp.Workbooks.Add
Set xlWS = xlWB.Worksheets.Add
'etc.


A better way (because it's early-bound):

Set a reference to the Excel Library (Menu: Project/References, scroll to
Microsoft Excel xx.x Object Library)

Dim xlApp As Excel.Application
Dim xlWkBk As Excel.Workbook
Dim xlWkSht As Excel.Worksheet

Set xlApp = New Excel.Application
xlApp.Visible = True 'as needed.
Set xlWkBk = xlApp.Workbooks.Add
Set xlWkSht = xlWkBk.Worksheets(0)

There will always be at least one worksheet in a new workbook, so you don't
need to add one unless you need more than one worksheet. The number of
sheets in the new workbook will depend on the user's setting, and can range
from 1 to 255 in Excel 2003.

The only problem is if the user has an earlier version of Excel and you use
a method or property that has been introduced since, your app will crash on
their machine.

Austin