PC Review


Reply
Thread Tools Rate Thread

Access data in a closed workbook with ExecuteExcel4Macro

 
 
lisamariechemistry@yahoo.com
Guest
Posts: n/a
 
      29th Feb 2008
Hi -

I'm trying to access data from a cell in a closed workbook, so I read
previous posts about it and started with a "simplest case" test, which
works fine:


debug.print ExecuteExcel4Macro("'c:\[TEST.xls]Sheet1'!R1C1")
'yields the value of cell A1
'with TEST.xls as a new workbook created by me - nothing done to it
except to add some text in cell A1

BUT: when i try it with the file I actually want to access, I
encounter problems...

debug.print ExecuteExcel4Macro("'c:\[SURR01.xls]Sheet1'!R1C1")
'yields Error 2023

However, if c:\SURR01.xls is open, ExecuteExcel4Macro does yield the
correct results.

I've triple and quadruple checked my syntax, looking for any typos,
and Sheet1 is definitely a sheet in the workbook. So: what else
could be different about this workbook to cause the error? It isn't
protected. It is generated by specialized software (HP CHEMSTATION)
but as far as I can tell is a normal (and fairly simple) excel
workbook, nothing but text in the cells.

If need be, I can easily write the program to open all the files to
access the data, but I'm thinking somebody out there knows the answer
to my mystery. Thanks! -Lisa
 
Reply With Quote
 
 
 
 
Kenneth Hobson
Guest
Posts: n/a
 
      29th Feb 2008
Maybe this will help.

Private Function GetInfoFromClosedFile(ByVal wbPath As String, wbName As
String, wsName As String, cellRef As String) As Variant
Dim arg As String
GetInfoFromClosedFile = ""
If Right(wbPath, 1) <> "\" Then wbPath = wbPath & "\"
If Dir(wbPath & "\" & wbName) = "" Then Exit Function
arg = "'" & wbPath & "[" & wbName & "]" & wsName & "'!" &
Range(cellRef).Address(True, True, xlR1C1)
On Error Resume Next
GetInfoFromClosedFile = ExecuteExcel4Macro(arg)

End Function
 
Reply With Quote
 
Rick Rothstein \(MVP - VB\)
Guest
Posts: n/a
 
      29th Feb 2008
My guess is that SURR01.xls is in another directory from where your
documents are stored by default. Try putting the whole path to it in front
of the left square bracket, something like this...

Debug.Print ExecuteExcel4Macro("'C:\Dir1\Dir2\[SURR01.xls]Sheet1'!R1C1")

Replacing the C:\Dir1\Dir2 with whatever directory path your file is stored
at.

Rick


<(E-Mail Removed)> wrote in message
news:277889ab-240b-48d4-b7a9-(E-Mail Removed)...
> Hi -
>
> I'm trying to access data from a cell in a closed workbook, so I read
> previous posts about it and started with a "simplest case" test, which
> works fine:
>
>
> debug.print ExecuteExcel4Macro("'c:\[TEST.xls]Sheet1'!R1C1")
> 'yields the value of cell A1
> 'with TEST.xls as a new workbook created by me - nothing done to it
> except to add some text in cell A1
>
> BUT: when i try it with the file I actually want to access, I
> encounter problems...
>
> debug.print ExecuteExcel4Macro("'c:\[SURR01.xls]Sheet1'!R1C1")
> 'yields Error 2023
>
> However, if c:\SURR01.xls is open, ExecuteExcel4Macro does yield the
> correct results.
>
> I've triple and quadruple checked my syntax, looking for any typos,
> and Sheet1 is definitely a sheet in the workbook. So: what else
> could be different about this workbook to cause the error? It isn't
> protected. It is generated by specialized software (HP CHEMSTATION)
> but as far as I can tell is a normal (and fairly simple) excel
> workbook, nothing but text in the cells.
>
> If need be, I can easily write the program to open all the files to
> access the data, but I'm thinking somebody out there knows the answer
> to my mystery. Thanks! -Lisa


 
Reply With Quote
 
lisamariechemistry@yahoo.com
Guest
Posts: n/a
 
      29th Feb 2008
Rick - that's definitely not the problem! I tried that first, but
since the directory string is long and complicated, I wanted to
elimiate that as a possible source of error and therefore copied the
file to my c directory.
 
Reply With Quote
 
Rick Rothstein \(MVP - VB\)
Guest
Posts: n/a
 
      29th Feb 2008
Unfortunately, then, I cannot duplicate your problem on my system. Moving
the file into the root of C: worked fine for me... your statement reported
the contents of the cell from the closed file with no problem. One thing I
found interesting though... **before** I move the file, I tried your
statement both with and without the "C:\" part and, in both cases, if found
the file in the default directory. That really surprised me... prefixing
"C:\" in front of the left square bracket had no effect on the file being
found. I'm guessing Excel looks in the default directory if it can't find
the file in the specified location (or is it the reverse of this... it looks
in the default location first?).

Rick


<(E-Mail Removed)> wrote in message
news:1d683243-bb38-450f-8480-(E-Mail Removed)...
> Rick - that's definitely not the problem! I tried that first, but
> since the directory string is long and complicated, I wanted to
> elimiate that as a possible source of error and therefore copied the
> file to my c directory.


 
Reply With Quote
 
lisamariechemistry@yahoo.com
Guest
Posts: n/a
 
      29th Feb 2008
Well, I've tested out Kenneth's function now. As I suspected, it's
just a longer way of reaching the same result that I was getting:
which is that it seems to work fine for any file anywhere in my
network except these files generated by the chemstation software,
leading me to believe there is some difference in the SURR01.xls
workbook itself, though I can't find any. Okay, well thanks for
trying. I guess It's back to opening hundreds of files for me! -Lisa

Oh by the way, there is an error in Kenneth's function.

If Right(wbPath, 1) <> "\" Then wbPath = wbPath & "\"
If Dir(wbPath & "\" & wbName) = "" Then Exit Function

You're adding "\" twice

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting data from a closed workbook =?Utf-8?B?QmFhcGk=?= Microsoft Excel Programming 5 11th Apr 2006 07:52 AM
add data to a closed workbook =?Utf-8?B?U3BlbmNlciBIdXR0b24=?= Microsoft Excel Programming 2 9th Apr 2005 07:16 PM
How to call Macro(using ExecuteExcel4Macro) for a particular workbook bhagwandas@gmail.com Microsoft Excel Programming 6 22nd Dec 2004 11:23 AM
PUT data in closed workbook Max Potters Microsoft Excel Programming 5 18th Aug 2004 01:33 PM
getting data from closed workbook keyur Microsoft Excel Programming 3 2nd Apr 2004 09:10 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:16 PM.