Solved

checking file exists

Posted on 1998-08-15
1
155 Views
Last Modified: 2010-04-30
if file exists i want to delete it or overwrite it
or save it as another filename
how do i check for the file exists or not and delete it in my dir
or save as different filename in excel? how do i do this in vb?
0
Comment
Question by:nightrage
1 Comment
 
LVL 1

Accepted Solution

by:
soeding earned 50 total points
ID: 1429079
nightrage,

If Dir(App.Path & "\filename.xls") <> "" Then
' If FileLen(App.Path & "\filename.xls") <> 0 Then
  ' Save it as another file
  FileCopy App.Path & "\filename.xls", "c:\temp\newfilename.xls"
  Kill App.Path & App.Path & "\filename.xls"
End If

' Delete safe (waste paper basket)
Private Const FO_DELETE = &H3
Private Const FOF_ALLOWUNDO = &H40
Private Const FOF_NOCONFIRMATION = &H70
Type SHFILEOPSTRUCT
    hWnd As Long
    wFunc As Long
    pFrom As String
    pTo As String
    fFlags As Integer
    fAborted As Boolean
    hNameMaps As Long
    sProgress As String
End Type
Private Declare Function SHFileOperation Lib "shell32.dll" Alias _
  "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long

Private Function SafeDelete(ParamArray files() As Variant)
  Dim i As Integer, fileNames$
  Dim SHFileOp As SHFILEOPSTRUCT
  For i = LBound(files) To UBound(files)
    fileNames = fileNames & files(i) & vbNullChar
  Next
  fileNames = fileNames & vbNullChar
  With SHFileOp
    .wFunc = FO_DELETE
    .pFrom = fileNames
    .fFlags = FOF_ALLOWUNDO
    .fFlags = FOF_NOCONFIRMATION
  End With
  SafeDelete = SHFileOperation(SHFileOp)
End Function

SafeDelete App.Path & "\filename.xls"


Regards
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now