Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between CreateObject("Excel.Application") .Workbooks.Open and just Workbooks.Open

Tags:

excel

vba

I am currently using Workbooks.Open to process a large number of files contained in a directory. But opening and closing these files make the files appear in the task bar and I wanted to avoid the constant flickering.

I got a suggestion from that I can use CreateObject to create a new instance since that opens a new Excel instance which is hidden.

  1. Is there any other difference between the two ways of opening new workbooks in terms of performance?

  2. Also, should I just use one instance of Excel created using CreateObject to open all Workbooks or do I need to create one instance for each workbook I have to process (which seems like a waste of lot of memory and less speed)?

like image 402
Siraj Samsudeen Avatar asked Oct 24 '12 07:10

Siraj Samsudeen


People also ask

What is the difference between workbook and worksheet in VBA?

A "Workbook" has one or more "Worksheets" - the collection, whilst a specific object from the collection is a "Worksheet". In your example, you are trying to select a worksheet from the collection by name, but since one worksheet by itself isn't a worksheet collection, it will not work. When you do ThisWorkbook.

What is open workbook?

Toggle navigation. Opening a workbook lets you use a workbook that you or someone else has previously created and then saved.

What is Application object in Excel?

The Application object contains: Application-wide settings and options. Methods that return top-level objects, such as ActiveCell, ActiveSheet, and so on.


2 Answers

Workbooks.Open uses the current MS Excel instance and CreateObject(“Excel.Application”) creates a new MS Excel instance. You can read up on CreateObject here.

Simply issuing a Workbooks.Open after creating a new instance will not ensure that the workbooks open in the new instance. You will have to bind with it. For example

Dim oXLApp As Object, wb As Object

Set oXLApp = CreateObject("Excel.Application")

'~~> Hide Excel
oXLApp.Visible = False

'~~> Open files
Set wb = oXLApp.Workbooks.Open("C:\Sample.xls")

Regarding your other question

Also, should I just use one instance of Excel created using CreateObject to open all Workbooks or do I need to create one instance for each workbook I have to process

You don't need several instances. You can work with one instance. For example

Dim oXLApp As Object, wb As Object

Set oXLApp = CreateObject("Excel.Application")

'~~> Hide Excel
oXLApp.Visible = False

'~~> Open files
Set wb = oXLApp.Workbooks.Open("C:\Sample1.xls")

'
'~~> Do some Stuff
'

wb.Close (False)

'~~> Open files
Set wb = oXLApp.Workbooks.Open("C:\Sample2.xls")
'
'~~> Do some Stuff
'

wb.Close (False)

'
'~~> And So on
'
like image 90
Siddharth Rout Avatar answered Sep 21 '22 14:09

Siddharth Rout


Late binding is slightly slower than early binding, but you may not even notice the difference. Yes, you can use just use one instance for all the workbooks. Note that this:

Dim xl As New Excel.Application

xl.Workbooks.Open "z:\docs\test.xlsm"

Will not be visible unless you say:

xl.Visible = True

Be sure to close any instances in your error trap.

like image 40
Fionnuala Avatar answered Sep 23 '22 14:09

Fionnuala