1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

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

Discussion in 'Software Development' started by balloon_tom, Apr 11, 2008.

Thread Status:
Not open for further replies.
Advertisement
  1. balloon_tom

    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
     
  2. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
  3. balloon_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
     
  4. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    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.
     
  5. balloon_tom

    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!
     
  6. Zack Barresse

    Zack Barresse

    Joined:
    Jul 25, 2004
    Messages:
    5,452
    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
     
  7. Sponsor

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 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/702633

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice