If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > PC based Database Applications > Microsoft Excel > error while trying to kill a file in excel

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-20-04, 09:33
Alexxx12 Alexxx12 is offline
Registered User
 
Join Date: Sep 2002
Location: NJ
Posts: 139
error while trying to kill a file in excel

Hi,

I get an error when I run the routine bellow.

strfpath = "\\USERSTATS\" & Range("C1").Value & ".xls"
If Dir(strfpath) Then
Kill strfpath
End If

the error is;

Run-Time error '13'
Type Mismatch

When I click on debug, the
"If Dir(strfpath) Then"
is hilighted

Note that the path is correct and when I bring the cursor on the highlited part, it gives me the entire path with the correct file name.

Thank you for your anticipated help.
Alexxx
Reply With Quote
  #2 (permalink)  
Old 08-22-04, 22:24
savbill savbill is offline
Registered User
 
Join Date: Feb 2004
Posts: 533
'Dir(strfpath)' is going to return a string value of the filename or an empty string so if true/false condition is ging to cause an error.

Need to check for a string value.
If Dir(strfpath) <> "" Then Kill strfpath
or you could use this variation
If Not Dir(strfpath) = "" Then kill strfpath

another way is to use a function like:

Sub killFile()
Dim strfpath As String

strfpath = "c:\" & Range("C1").Value & ".xls"

If FileExists(strfpath) Then
Kill strfpath
Debug.Print "Killed the File " & Range("C1").Value & ".xls"
End If
End Sub


Private Function FileExists(fname) As Boolean
' Returns TRUE if the file exists
Dim x As String
x = Dir(fname)
If x <> "" Then FileExists = True _
Else FileExists = False
End Function
__________________
~

Bill
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On