Solved

checking file exists

Posted on 1998-08-15
1
154 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

747 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

12 Experts available now in Live!

Get 1:1 Help Now