Solved

Manually Opened Excel Instance Interferes with Excel OLE Automation from VB

Posted on 2007-03-26
7
205 Views
Last Modified: 2013-11-26
Hi,

I have a problem with OLE Automation with Excel from VB 6.0 Pro.  I have several procedures that send data to Excel to create reports.  Some of them take a little while to create and the users work on other things while they are running.  If they go to My Computer / Explorer and double-click on an Excel file it will open in the instance of Excel VB opened and interfere with the report running.  This is how I declare my Excel related variables:

        Set oxl = CreateObject("Excel.application")
        Set oBook = oxl.Workbooks.add()
        Set oSheet = oBook.Sheets("Sheet1")

Is there any way to avoid this problem, other than tying the users' hands down while their reports are generating?

Thanks!
0
Comment
Question by:tlchavet
  • 4
  • 3
7 Comments
 
LVL 13

Expert Comment

by:nike_golf
ID: 18794733
More than likely you'll want to use early binding vs. late binding to solve some of those issues. Take a look at this thread it has some good examples and is a great source of information.

http://www.experts-exchange.com/Programming/Languages/Visual_Basic/Q_20313838.html?sfQueryTermInfo=1+bind+earli+excel

NG,
0
 
LVL 1

Author Comment

by:tlchavet
ID: 18794846
Hi,
Sorry I should have included my dims:
    Dim oxl As Excel.Application
    Dim oBook As Excel.Workbook
    Dim oSheet As Excel.worksheet
I am using early binding.
0
 
LVL 13

Expert Comment

by:nike_golf
ID: 18795146
Try using the keyword "New"

Dim oxl As New Excel.Application

or

Set oxl = New Excel.Application

NG,
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:tlchavet
ID: 18799805
Hi,

I tried that.  The user still picks up the instance opened by VB if they double-click another Excel worksheet to open.  Is there a way to have VB open it's instance "exclusively".

Thanks
0
 
LVL 13

Accepted Solution

by:
nike_golf earned 500 total points
ID: 18810133
Well the problem is more of an Excel problem than a coding problem.

Using the following:

Dim oXL As Object
Dim oBook As Object
Dim oSheet As Object

Private Sub Command1_Click()
Set oXL = New Excel.Application
Set oBook = oXL.Workbooks.Add()
Set oSheet = oBook.Sheets("Sheet1")
oXL.Visible = True
oXL.WindowState = xlMinimized

MsgBox "Excel Open"

Set oSheet = Nothing
Set oBook = Nothing

oXL.Quit
Set oXL = Nothing

MsgBox "Excel Closed"

End Sub


This sub does create a separate instance of Excel, the problem I see is that when you open an additional workbook after your code is running Excel uses that instance to open the workbook in. However, if you perform the operation in reverse order it keeps the instances separate, a "New" instance is created for your application.

My guess would be that Excel is using the GetObject() function by default when opening a new workbook...

NG,
0
 
LVL 1

Author Comment

by:tlchavet
ID: 18811033
Thanks - the problem then boils down to the users needing to be patient.  Thanks a bunch for the assistance!
0
 
LVL 13

Expert Comment

by:nike_golf
ID: 18811270
Sorry, I couldn't give you the answer you were looking for...

NG,
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question