# Solved: VBA Excel; Data transfer between 2 different open Apps



## balloon_tom (Jan 5, 2006)

I have 2 Excel workbooks open at the same time, each within their own separate applications. Each was started using the following 2 shortcuts:

"C:\Program Files\Microsoft Office\OFFICE11\EXCEL.EXE" C:\FltTools\Book1_.xls
and
"C:\Program Files\Microsoft Office\OFFICE11\EXCEL.EXE" C:\FltTools\Book2_.xls.

Assume that Book2 has been modified since it's last save
and
Book1, which is active, needs a copy of the modified worksheet from Book2.

First, I'll check to see if Book2 is open, and then if not, open it:

On Error Resume Next
Windows(C:\FltTools\book2_.xls).activate
If Err.Number>0 then Workbooks.Open (C:\FltTools\Book2_.xls)

PROBLEM: Book1's application is not seeing that Book2 is already open, most likely because it is in a different application. So, this code opens a new instance of the last saved version of Book2 into Book1's application, and is then copying the older unmodified worksheet.

QUESTION: How can I code one application to search for an open workbook within a different application?

Thank you, Tom


----------



## Chicon (Jul 29, 2004)

Hi balloon_tom,

You should detect first if the file is already used : Detecting an Open File.


----------



## balloon_tom (Jan 5, 2006)

Excellant, Chicon. This code does indeed check to see if the workbook is open anywhere in the computer and as you have said, this is step one.

I'm now up to the code below which properly detects the 3 possible conditions of Book2:

dName = "C:\FltTools\"
fName = "Book2_.xls"
On Error Resume Next
Windows(fName).Activate
If Err.Number > 0 Then
On Error GoTo 0
If Not IsFileAlreadyOpen(dName & fName) Then
Workbooks.Open (dName & fName)
Else
Windows(dName & fName).Activate 'this does not work
End If
End If

Condition1: Book2 is open and within the same application as Book1. In this case I can copy Book2 data into Book1. This works!

Condition2: Book2 is not open anywhere. In this case Book2 is then opened into the same app as Book1 and the data copy works!

Condition3: THIS ONE DOESN'T WORK. Book2 is open but in another application. This is my problem now. How do I (or is it even possible) to copy data from the already opened Book2? Windows(dName & fName).Activate returns Subscript out of range.

Tom


----------



## Chicon (Jul 29, 2004)

balloon_tom said:


> Condition3: THIS ONE DOESN'T WORK. Book2 is open but in another application. This is my problem now. How do I (or is it even possible) to copy data from the already opened Book2? Windows(dName & fName).Activate returns Subscript out of range.


I'm not a VB expert but I don't think it is possible to simultaneously run 2 or more Excel processes sharing the same file.
There's another option : you can make application1 wait until application2 has freed the file using a timer.
In VB, you can use a timer you set with a short time interval and while treating a tick event, your application checks the status of the file. As soon, the file is free, the timer is stopped and the copy procedure may start.


----------



## balloon_tom (Jan 5, 2006)

I have it working, but not by any simple approach. A lot of tests for various scenarios...but it's only code!


----------



## Zack Barresse (Jul 25, 2004)

Excel is a single-instance application. You can have multiple files (workbooks) open in the same single instance, but you should NOT open more than one instance. This will tell you if the workbook is open or not, returning a boolean result...


```
Public Function WbOpen(wbName As String) As Boolean
'// Written by Zack Barresse, aka firefytr
'// Originally coded by Jake Marx
    On Error Resume Next
    WbOpen = Len(Excel.Workbooks(wbName).Name)
End Function
```
HTH


----------

