Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 78
  • Last Modified:

Workbooks.Open File already exists in this location

Using the following code to open a file.  Issue is popup alert that says the file already exists.... do I want to Save?  Yes/No/Cancel.  When I click Yes it does not open the file, when I press No or Cancel, I get a cannot access file?

I can surpress the Alert, but the file does not open...  when I do not surpress it is back to the alert.  How do I get around this and just open the file up?  Please advise and thanks.

'Application.DisplayAlerts = False
    
Workbooks.Open Filename:= _
"\\nahollap548\HP-UFT\ScriptResourceFiles\ProdOrdsOperListSeq.xls"

Open in new window

0
RWayneH
Asked:
RWayneH
  • 2
1 Solution
 
NorieCommented:
Is it possible the file is open?

If it is you could check for that before trying to open it.
0
 
RWayneHAuthor Commented:
Nope file is closed...  Do you have a code that would check?  If it is open display it or something?  Right now there is nothing that indicates that it is.
0
 
Ejgil HedegaardCommented:
The only way to test if a file is open is to try to open it.
This function does that, and returns True if the file is open.
Function FileLocked(strFileName As String) As Boolean
   On Error Resume Next
   Open strFileName For Binary Access Read Write Lock Read Write As #1
   Close #1
   If Err.Number <> 0 Then
      FileLocked = True
      Err.Clear
   End If
End Function

Open in new window


Use it like this
If FileLocked( Path and file name here ) Then
 'Display message that the file is open
Else
 'Open the file
End if

Open in new window

0
 
RWayneHAuthor Commented:
By checking if the file was open first it showed that I was opening the file twice.  It was already open.
Thanks for the help.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now