Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

checking file exists

Posted on 1998-08-15
1
Medium Priority
?
161 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
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…
Suggested Courses

715 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