PC Review


Reply
Thread Tools Rate Thread

Check for File Already Opened - Can't Get to Work

 
 
=?Utf-8?B?UGFpZ2U=?=
Guest
Posts: n/a
 
      29th May 2007
Have sub below that is used to get data from closed workbook (i.e., the
source file); it actually opens the file in read-only then closes again; it
works great. However, I can’t seem to integrate into it code to check to see
if the file which the user selects in the file dialog is already open or not.
Have tried functions, etc., but I think my problem could be that it is
opening the selected file prior to checking if it is open….just a guess
anyway. Is it possible to have Excel check to see if the file selected by
the user is open already, and if it is, then somehow allow the user to select
the source file in the Excel window, at which time Excel will pull in the
data that way? If it is not open, it would proceed as normal with the code
below. If not, then how can I modify it to just check if the file is open?

Dim WB As Workbook
Set WB = Workbooks.Open(Application.GetOpenFilename(FileFilter:="All
Files(*.*),*.xls,All Files (*.*),*.*"), True, True)
Application.ScreenUpdating = False
With ThisWorkbook.Worksheets("Input and P&L")
.Range("E9").Value = WB.Worksheets("Input and P&L").Range("e9").Value
.Range("E10").Value = WB.Worksheets("Input and
P&L").Range("e10").Value
End With
WB.Close False
Set WB = Nothing
Application.ScreenUpdating = True
End Sub



 
Reply With Quote
 
 
 
 
=?Utf-8?B?SmltIFRob21saW5zb24=?=
Guest
Posts: n/a
 
      29th May 2007
how about something like this perhaps (untested)

Dim WB As Workbook
Dim strFileName As String

strFileName = Application.GetOpenFilename(FileFilter:="All
Files(*.*),*.xls,All Files (*.*),*.*")

On Error Resume Next
Set WB = Workbooks(Mid(strFileName, InStrRev(strFileName, "\") + 1, 256))
On Error GoTo 0

If WB Is Nothing Then Set WB = Workbooks.Open(strFileName, True, True)

Application.ScreenUpdating = False
With ThisWorkbook.Worksheets("Input and P&L")
.Range("E9").Value = WB.Worksheets("Input and P&L").Range("e9").Value
.Range("E10").Value = WB.Worksheets("Input and
P&L").Range("e10").Value
End With
WB.Close False
Set WB = Nothing
Application.ScreenUpdating = True
End Sub

--
HTH...

Jim Thomlinson


"Paige" wrote:

> Have sub below that is used to get data from closed workbook (i.e., the
> source file); it actually opens the file in read-only then closes again; it
> works great. However, I can’t seem to integrate into it code to check to see
> if the file which the user selects in the file dialog is already open or not.
> Have tried functions, etc., but I think my problem could be that it is
> opening the selected file prior to checking if it is open….just a guess
> anyway. Is it possible to have Excel check to see if the file selected by
> the user is open already, and if it is, then somehow allow the user to select
> the source file in the Excel window, at which time Excel will pull in the
> data that way? If it is not open, it would proceed as normal with the code
> below. If not, then how can I modify it to just check if the file is open?
>
> Dim WB As Workbook
> Set WB = Workbooks.Open(Application.GetOpenFilename(FileFilter:="All
> Files(*.*),*.xls,All Files (*.*),*.*"), True, True)
> Application.ScreenUpdating = False
> With ThisWorkbook.Worksheets("Input and P&L")
> .Range("E9").Value = WB.Worksheets("Input and P&L").Range("e9").Value
> .Range("E10").Value = WB.Worksheets("Input and
> P&L").Range("e10").Value
> End With
> WB.Close False
> Set WB = Nothing
> Application.ScreenUpdating = True
> End Sub
>
>
>

 
Reply With Quote
 
=?Utf-8?B?UGFpZ2U=?=
Guest
Posts: n/a
 
      29th May 2007
Thanks, Jim - works great! Couple of questions:
1) Set WB = Workbooks(Mid(strFileName, InStrRev(strFileName, "\") + 1,
256))...am not sure I understand what you're doing here...extracting the name
only (versus the entire path) of the workbook?
2) If the file is already open, can I just extract the data and leave the
file open? I don't want to automatically close the file because they may
need to save it first or keep it open. Tried to modify it as follows, but
get a subscript out of range:

Dim WB As Workbook
Dim strFileName As String

strFileName = Application.GetOpenFilename(FileFilter:="All
Files(*.*),*.xls,All Files (*.*),*.*")

On Error Resume Next
Set WB = Workbooks(Mid(strFileName, InStrRev(strFileName, "\") + 1, 256))
On Error GoTo 0

If WB Is Nothing Then
Set WB = Workbooks.Open(strFileName, True, True)
Application.ScreenUpdating = False
With ThisWorkbook.Worksheets("Input and P&L")
.Range("E9").Value = WB.Worksheets("Input and
P&L").Range("e9").Value
.Range("E10").Value = WB.Worksheets("Input and
P&L").Range("e10").Value
End With
WB.Close False
Set WB = Nothing
Application.ScreenUpdating = True
Else
Set WB = Workbooks(strFileName)
Application.ScreenUpdating = False
With ThisWorkbook.Worksheets("Input and P&L")
.Range("E9").Value = WB.Worksheets("Input and
P&L").Range("e9").Value
.Range("E10").Value = WB.Worksheets("Input and
P&L").Range("e10").Value
End With
Set WB = Nothing
Application.ScreenUpdating = True
End If


"Jim Thomlinson" wrote:

> how about something like this perhaps (untested)
>
> Dim WB As Workbook
> Dim strFileName As String
>
> strFileName = Application.GetOpenFilename(FileFilter:="All
> Files(*.*),*.xls,All Files (*.*),*.*")
>
> On Error Resume Next
> Set WB = Workbooks(Mid(strFileName, InStrRev(strFileName, "\") + 1, 256))
> On Error GoTo 0
>
> If WB Is Nothing Then Set WB = Workbooks.Open(strFileName, True, True)
>
> Application.ScreenUpdating = False
> With ThisWorkbook.Worksheets("Input and P&L")
> .Range("E9").Value = WB.Worksheets("Input and P&L").Range("e9").Value
> .Range("E10").Value = WB.Worksheets("Input and
> P&L").Range("e10").Value
> End With
> WB.Close False
> Set WB = Nothing
> Application.ScreenUpdating = True
> End Sub
>
> --
> HTH...
>
> Jim Thomlinson
>
>
> "Paige" wrote:
>
> > Have sub below that is used to get data from closed workbook (i.e., the
> > source file); it actually opens the file in read-only then closes again; it
> > works great. However, I can’t seem to integrate into it code to check to see
> > if the file which the user selects in the file dialog is already open or not.
> > Have tried functions, etc., but I think my problem could be that it is
> > opening the selected file prior to checking if it is open….just a guess
> > anyway. Is it possible to have Excel check to see if the file selected by
> > the user is open already, and if it is, then somehow allow the user to select
> > the source file in the Excel window, at which time Excel will pull in the
> > data that way? If it is not open, it would proceed as normal with the code
> > below. If not, then how can I modify it to just check if the file is open?
> >
> > Dim WB As Workbook
> > Set WB = Workbooks.Open(Application.GetOpenFilename(FileFilter:="All
> > Files(*.*),*.xls,All Files (*.*),*.*"), True, True)
> > Application.ScreenUpdating = False
> > With ThisWorkbook.Worksheets("Input and P&L")
> > .Range("E9").Value = WB.Worksheets("Input and P&L").Range("e9").Value
> > .Range("E10").Value = WB.Worksheets("Input and
> > P&L").Range("e10").Value
> > End With
> > WB.Close False
> > Set WB = Nothing
> > Application.ScreenUpdating = True
> > End Sub
> >
> >
> >

 
Reply With Quote
 
=?Utf-8?B?UGFpZ2U=?=
Guest
Posts: n/a
 
      29th May 2007
Yikes, think I answered my own question #2. If I change
Else
Set WB = Workbooks(strFileName)
to:
Else
Set WB = Workbooks(Mid(strFileName, InStrRev(strFileName, "\") + 1,
256))

it pulls from the open file and leaves it open.

"Paige" wrote:

> Thanks, Jim - works great! Couple of questions:
> 1) Set WB = Workbooks(Mid(strFileName, InStrRev(strFileName, "\") + 1,
> 256))...am not sure I understand what you're doing here...extracting the name
> only (versus the entire path) of the workbook?
> 2) If the file is already open, can I just extract the data and leave the
> file open? I don't want to automatically close the file because they may
> need to save it first or keep it open. Tried to modify it as follows, but
> get a subscript out of range:
>
> Dim WB As Workbook
> Dim strFileName As String
>
> strFileName = Application.GetOpenFilename(FileFilter:="All
> Files(*.*),*.xls,All Files (*.*),*.*")
>
> On Error Resume Next
> Set WB = Workbooks(Mid(strFileName, InStrRev(strFileName, "\") + 1, 256))
> On Error GoTo 0
>
> If WB Is Nothing Then
> Set WB = Workbooks.Open(strFileName, True, True)
> Application.ScreenUpdating = False
> With ThisWorkbook.Worksheets("Input and P&L")
> .Range("E9").Value = WB.Worksheets("Input and
> P&L").Range("e9").Value
> .Range("E10").Value = WB.Worksheets("Input and
> P&L").Range("e10").Value
> End With
> WB.Close False
> Set WB = Nothing
> Application.ScreenUpdating = True
> Else
> Set WB = Workbooks(strFileName)
> Application.ScreenUpdating = False
> With ThisWorkbook.Worksheets("Input and P&L")
> .Range("E9").Value = WB.Worksheets("Input and
> P&L").Range("e9").Value
> .Range("E10").Value = WB.Worksheets("Input and
> P&L").Range("e10").Value
> End With
> Set WB = Nothing
> Application.ScreenUpdating = True
> End If
>
>
> "Jim Thomlinson" wrote:
>
> > how about something like this perhaps (untested)
> >
> > Dim WB As Workbook
> > Dim strFileName As String
> >
> > strFileName = Application.GetOpenFilename(FileFilter:="All
> > Files(*.*),*.xls,All Files (*.*),*.*")
> >
> > On Error Resume Next
> > Set WB = Workbooks(Mid(strFileName, InStrRev(strFileName, "\") + 1, 256))
> > On Error GoTo 0
> >
> > If WB Is Nothing Then Set WB = Workbooks.Open(strFileName, True, True)
> >
> > Application.ScreenUpdating = False
> > With ThisWorkbook.Worksheets("Input and P&L")
> > .Range("E9").Value = WB.Worksheets("Input and P&L").Range("e9").Value
> > .Range("E10").Value = WB.Worksheets("Input and
> > P&L").Range("e10").Value
> > End With
> > WB.Close False
> > Set WB = Nothing
> > Application.ScreenUpdating = True
> > End Sub
> >
> > --
> > HTH...
> >
> > Jim Thomlinson
> >
> >
> > "Paige" wrote:
> >
> > > Have sub below that is used to get data from closed workbook (i.e., the
> > > source file); it actually opens the file in read-only then closes again; it
> > > works great. However, I can’t seem to integrate into it code to check to see
> > > if the file which the user selects in the file dialog is already open or not.
> > > Have tried functions, etc., but I think my problem could be that it is
> > > opening the selected file prior to checking if it is open….just a guess
> > > anyway. Is it possible to have Excel check to see if the file selected by
> > > the user is open already, and if it is, then somehow allow the user to select
> > > the source file in the Excel window, at which time Excel will pull in the
> > > data that way? If it is not open, it would proceed as normal with the code
> > > below. If not, then how can I modify it to just check if the file is open?
> > >
> > > Dim WB As Workbook
> > > Set WB = Workbooks.Open(Application.GetOpenFilename(FileFilter:="All
> > > Files(*.*),*.xls,All Files (*.*),*.*"), True, True)
> > > Application.ScreenUpdating = False
> > > With ThisWorkbook.Worksheets("Input and P&L")
> > > .Range("E9").Value = WB.Worksheets("Input and P&L").Range("e9").Value
> > > .Range("E10").Value = WB.Worksheets("Input and
> > > P&L").Range("e10").Value
> > > End With
> > > WB.Close False
> > > Set WB = Nothing
> > > Application.ScreenUpdating = True
> > > End Sub
> > >
> > >
> > >

 
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
Check if a file has been opened Wayne-I-M Microsoft Frontpage 1 21st Oct 2008 01:18 PM
code to check file size everytime an Excel file is opened Kaiser Microsoft Excel Programming 2 30th Jul 2006 05:46 PM
How to check if file is opened by other process =?Utf-8?B?d2FsdGVy?= Microsoft Dot NET 1 24th Feb 2006 08:16 PM
How to check if file has already been opened by another user =?Utf-8?B?SXZheWxvIEdlb3JnaWV2?= Microsoft C# .NET 0 29th Apr 2004 12:36 AM
how to check through Access whether some Excel file is opened Alex Microsoft Access External Data 2 20th Jan 2004 01:13 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:25 AM.