ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   How to create Excel workbook from VB6? (https://www.excelbanter.com/excel-programming/296637-how-create-excel-workbook-vb6.html)

Kurt Remlin

How to create Excel workbook from VB6?
 
Hi,

What do I need to add to VB6 project to be able to create Excel'97
document on the fly?

How do I get access to the same objects that are available from Excel
itself via VBA?

Is there a good source with examples for such stuff?

Thank you.

Bob Phillips[_6_]

How to create Excel workbook from VB6?
 
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.


--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Kurt Remlin" wrote in message
m...
Hi,

What do I need to add to VB6 project to be able to create Excel'97
document on the fly?

How do I get access to the same objects that are available from Excel
itself via VBA?

Is there a good source with examples for such stuff?

Thank you.




AustinMN

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


Bob Phillips[_6_]

How to create Excel workbook from VB6?
 

"AustinMN" wrote in message
...
"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):


It's horses for courses. Better in some instances, not so in others. It is
not black and white.


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.


And a later version may lose the reference (hence late bound)



AustinMN

How to create Excel workbook from VB6?
 
"Bob Phillips" wrote in message
...

"AustinMN" wrote in message
...
"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):


It's horses for courses. Better in some instances, not so in others. It is
not black and white.


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.


And a later version may lose the reference (hence late bound)


While that's possible (Micro$oft can break anything), I've never experienced
it. I've written over 100,000 lines of VBA specifically for Excel for 7
clients dating back to 1994. Not one line has broken because a method or
property has disappeared.

MS Outlook, on the other hand... =8-((

Austin


Bob Phillips[_6_]

How to create Excel workbook from VB6?
 


"AustinMN" wrote in message
...
"Bob Phillips" wrote in message
...

"AustinMN" wrote in message
...
"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):


It's horses for courses. Better in some instances, not so in others. It

is
not black and white.


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.


And a later version may lose the reference (hence late bound)


While that's possible (Micro$oft can break anything), I've never

experienced
it. I've written over 100,000 lines of VBA specifically for Excel for 7
clients dating back to 1994. Not one line has broken because a method or
property has disappeared.


That's not what I said. I mentioned lost references. And if you think that
doesn't happen, just hang out here a while and you will see many examples.




All times are GMT +1. The time now is 11:42 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com