Solved

Manually Opened Excel Instance Interferes with Excel OLE Automation from VB

Posted on 2007-03-26
7
200 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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 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…

770 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