Solved

Manually Opened Excel Instance Interferes with Excel OLE Automation from VB

Posted on 2007-03-26
7
195 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
Comment Utility
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
Comment Utility
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
Comment Utility
Try using the keyword "New"

Dim oxl As New Excel.Application

or

Set oxl = New Excel.Application

NG,
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 1

Author Comment

by:tlchavet
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Sorry, I couldn't give you the answer you were looking for...

NG,
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now