Avoid problem that file is already existing

Hi,
I want to save the current newly created Excel file like

wbTrg.SaveAs ThisWorkbook.Path & "\Departure List (of People already left).xlsx"

Open in new window


How to avoid the problem that, Departure List (of People already left).xlsx, maybe is already being opened?
LVL 12
HuaMin ChenProblem resolverAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Roy CoxGroup Finance ManagerCommented:
Here's a Function that will check if a file or folder exists, I think this was written by Ken Puls

Public Function FileFolderExists(sFullPath As String) As Boolean

    If Not Dir(sFullPath, vbDirectory) = vbNullString Then FileFolderExists = True

End Function

Public Sub TestFolderExistence()
 
    If FileFolderExists("C:\Accounts") Then
        MsgBox "Folder exists!"
    Else: MkDir ("C:\Accounts")
    End If

End Sub

Public Sub TestFileExistence()

    If FileFolderExists("C:\Accounts\TestWorkbook.xls") Then
        MsgBox "File exists!"
    Else
        MsgBox "File does not exist!"
    End If

End Sub

Open in new window


Or
Option Explicit

''///Check if the required excel files exist.
Private Sub ChkExcelFiles()
    Dim filPath As String, filName As String

    filName = "MyWorkBook"
    filPath = Dir(ThisWorkbook.Path & Application.PathSeparator & filName)

    ''///If there are no Excel files exit the sub
    If filPath = "" Then MsgBox "File not found - " & filName, vbCritical
  
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HuaMin ChenProblem resolverAuthor Commented:
Hi,
Your details is to see if relevant file is existing or not, right? How to detect if relevant file IS BEING opened?
0
Roy CoxGroup Finance ManagerCommented:
Try this, Im going out for a while but I'll check back later

Option Explicit
Function wbOpen(wbName As String) As Boolean
' returns TRUE if the workbook is open
    wbOpen = False
    On Error GoTo wbNotOpen
    If Len(Application.Workbooks(wbName).Name) > 0 Then
        wbOpen = True
        Exit Function
    End If
wbNotOpen:
End Function


Sub TestOpen()
If Not wbOpen("Suppliers.xls") Then    Workbooks.Open "Suppliers.xls"

End Sub

Open in new window

0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

HuaMin ChenProblem resolverAuthor Commented:
I put your event, and it cannot detect that Excel is being opened, by these codes

            Dim Fl0 As String: Fl0 = ThisWorkbook.Path & "\Departure List (of People already left).xlsx"
            If wbOpen(Fl0) Then
                MsgBox "Please close your current open file (Departure List (of People already left).xlsx) and then re-try.", vbOKOnly + vbExclamation
                Exit Function
            End If

Open in new window

0
Roy CoxGroup Finance ManagerCommented:
I have you included the Function in your code?

Try it withount the path as I have
0
Roy CoxGroup Finance ManagerCommented:
Looks like it worked. Pleased to help.
0
HuaMin ChenProblem resolverAuthor Commented:
Appreciated!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Office

From novice to tech pro — start learning today.