Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

checking file exists

Posted on 1998-08-15
1
Medium Priority
?
162 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 100 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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 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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

773 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