Solved

Manually Opened Excel Instance Interferes with Excel OLE Automation from VB

Posted on 2007-03-26
7
209 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

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…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

717 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