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

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

balloon_tom

Thread Starter
Joined
Jan 4, 2006
Messages
107
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
 

balloon_tom

Thread Starter
Joined
Jan 4, 2006
Messages
107
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
 
Joined
Jul 29, 2004
Messages
6,650
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

Thread Starter
Joined
Jan 4, 2006
Messages
107
I have it working, but not by any simple approach. A lot of tests for various scenarios...but it's only code!
 
Joined
Jul 25, 2004
Messages
5,458
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...

Code:
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
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Members online

Top