don't open workbook if it is already open (1 Viewer)

megatronixs

Registered User.
Local time
Today, 15:39
Joined
Aug 17, 2012
Messages
719
Hi all,

Today I have a voodoo day where all is hard to fix or find answer.
at the beginning I had only code to open one excel workbook and do something with it. Now because of new process, I have to do something more to it, so if the workbook is open, use it and don't open another.
I can't seem to find answer for this.

This below works, but when I need to run the second part via other button, it will open the workbook again and that one will be empty with not the values from previous code run:
Code:
    Set objXL = CreateObject("Excel.Application")
    
    objXL.Visible = True
    Set xlWB = objXL.Workbooks.Open("F:\Test\Downstream.xlsm")
    Set xlWS = xlWB.Worksheets("process")
     
    LastRow = xlWS.Cells(60000, 1).End(xlUp).Row
  
    For j = 2 To LastRow
    Acc = xlWS.Cells(j, 1).Value

Any idea to solve this? by the end of the day I seem not to get it.

Greetings.
 

Gasman

Enthusiastic Amateur
Local time
Today, 14:39
Joined
Sep 21, 2011
Messages
14,306
Not sure you can do that unless you are the one who has it open?, and then you should know.? :)

I've found this out in the past when I still had the Excel workbook open in Excel to check something and then was trying to open it in my access code (for update admittedly).

HTH
 

megatronixs

Registered User.
Local time
Today, 15:39
Joined
Aug 17, 2012
Messages
719
I'm actually the only one having it open with the first part of the code works. Just want to continue with it without opening it again :)

Greetings.
 

Solo712

Registered User.
Local time
Today, 09:39
Joined
Oct 19, 2012
Messages
828
Hi all,

Today I have a voodoo day where all is hard to fix or find answer.
at the beginning I had only code to open one excel workbook and do something with it. Now because of new process, I have to do something more to it, so if the workbook is open, use it and don't open another.
I can't seem to find answer for this.

This below works, but when I need to run the second part via other button, it will open the workbook again and that one will be empty with not the values from previous code run:
Code:
    Set objXL = CreateObject("Excel.Application")
    
    objXL.Visible = True
    Set xlWB = objXL.Workbooks.Open("F:\Test\Downstream.xlsm")
    Set xlWS = xlWB.Worksheets("process")
     
    LastRow = xlWS.Cells(60000, 1).End(xlUp).Row
  
    For j = 2 To LastRow
    Acc = xlWS.Cells(j, 1).Value

Any idea to solve this? by the end of the day I seem not to get it.

Greetings.

Hi you should use "GetObject" first which will refer to an existing instance of Excel and create the object only if none is found. Like this:

Code:
On Error Resume Next
    Set objXL = GetObject(, "Excel.Application")
    
    If Err.Number <> 0 Then
        Err.Clear
        'MsgBox "creating new Excel"
        Set objXL = CreateObject("Excel.Application")
    End If

Then you should not have the issue of multiple instances of the same workbook.

Best,
Jiri
 
Last edited:

Galaxiom

Super Moderator
Staff member
Local time
Today, 23:39
Joined
Jan 20, 2009
Messages
12,852
First get any Excel objects as jiri shows.

Then loop through their Workbooks Collections and test the FullName property for a match.
 

Users who are viewing this thread

Top Bottom