Solved

checking file exists

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

803 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