Hi All,
how does one read/copy data from a closed workbook (without opening it?)?
At present I am using code as follows, but don't wish to open the workbook.

  Set SrcWbk = Workbooks.Open("L:\Backup\BE.xlsx", , , , "test")
  Set SrcSht = SrcWbk.Worksheets("Sheet1")
  DestWbk.Worksheets("Sheet2").Range("A1").PasteSpecial xlPasteAll

Who is Participating?
NorieVBA ExpertCommented:
There are various ways to get data from a closed workbook.

Here's one.
Private Function GetValue(path, file, sheet, ref)
'   Retrieves a value from a closed workbook
Dim arg As String

    If Right(path, 1) <> "\" Then path = path & "\"

    If Dir(path & file) = "" Then
        GetValue = "File Not Found"
        Exit Function
    End If

    arg = "'" & path & "[" & file & "]" & sheet & "'!" & _
      Range(ref).Range("A1").Address(, , xlR1C1)

    GetValue = ExecuteExcel4Macro(arg)

End Function

Open in new window

This code only gets one value at time so probably isn't appropriate for a large amount of data but for 7 cells (B2:H2) should be OK to use in a loop.

Perhaps something like this.
Dim rngDst As Range

    Set rngDst = DestWbk.Worksheets("Sheet2").Range("A1")
    For Each cl in Range("B2:H2")

        rngDst = GetValue("L:\Backup\", "BE.xlsx", "Sheet1", cl.Address)
        Set rngDst = rngDst.Offset(,1)

    Next cl

Open in new window

You can't get data from a file that is not "open" in some fashion.  You could probably work around it somehow by reading in raw bytes or something...but why would you want to?

If you're using the excel libraries as it appears you are, you are doing it the right way.
COACHMAN99Author Commented:

it looks as if ExecuteExcel4Macro(arg) is the key. let me try it.
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

I'm sure the macro "opens" the file just like any other means of reading the data...the process will just be listed as some COM component or something.  And I've never really had MS Office COM behave like I want it to.

My 2 cents.
NorieVBA ExpertCommented:

The workbook is not being opened.
Jacques Bourgeois (James Burger)PresidentCommented:
First of all, you did not tell us where that code is running. In an Excel macro? From a script file? From a VB6 application? From a VB.NET application? That might change the answer a bit. I am assuming that you are not running the code from Excel. Otherwise, you will need to use one of the 3 other methods in order to make it work with an "apparent" closed file.

"apparent" because, as jmcmunn stated, there is no way to read from a file without opening it. No matter which technique you use, Excel has to fire in the background, although it can be hidden from the user.

One thing you can do is to set the Visible property of the Excel.Application object to False, so the application does not show on the screen. This makes the process transparent to the user, and also helps performance because Excel does not have to work on the display. In such a situation however, you must be sure to trap Exceptions carefully and make sure that the Application is closed when you are finished with it, because the user does not know that Excel is running in the background and does not have the usual interface to Close it in case the application crash.

One thing that could improve the performance however, if you have that problem and if it suits the situation, is to do most of the
COACHMAN99Author Commented:
Hi imnorie,
what data-type is cl? (couldn't find 'cell')
would you be able to place actual values in the cl.Address arg so I can 'see-monkey..'?
COACHMAN99Author Commented:
Hi jamesburger,
thanks for that. I would probably have to 'hide' the source file, but leave the des open (as this is where the code is run from)

any idea what property only applies to the workbook, not the application?
NorieVBA ExpertCommented:
cl shoud be declared as range.

Not sure what you mean about actual values of cl.Address, that's just the cell address of cl, ie $B$2, $C$2, $D$2,...,$H$2.

I could actually have used an array or some other method for that but using  a range and VBA's built-in Address property seemed easier.
NorieVBA ExpertCommented:

How will you hide the source file when it's not being opened?
COACHMAN99Author Commented:
thanks guys

P.S. imnorie. how do I pass the password in the ExecuteExcel4Macro arg?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.