Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
Existing file ?
Im after some code that will check to see if a file exists in a specifi location. Ive been given the below two examples but i cant get them to work. example 1 if Not Dir("C:\DATA\YOURFILE.XLS") then 'the bugger isnot there! end if example2 Sub DoesFileExist() Set MyFile = CreateObject("Scripting.FileSystemObject") MsgBox MyFile.FileExists("c:\filename.xls") End Su -- ceem ----------------------------------------------------------------------- ceemo's Profile: http://www.excelforum.com/member.php...fo&userid=1065 View this thread: http://www.excelforum.com/showthread.php?threadid=40039 |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
Existing file ?
Hi ceemo,
This works for me - Sub test() Dim bGotIt As Boolean Dim sFile As String Dim sPath As String sFile = "Book1.xls" sPath = "c:\temp\" 'On Error Resume Next bGotIt = (LCase(sFile) = LCase(Dir(sPath & sFile))) 'On Error GoTo 0 MsgBox bGotIt End Sub I though that would need error handling if false, but doesn't seem necessary. Regards, Peter T "ceemo" wrote in message ... Im after some code that will check to see if a file exists in a specific location. Ive been given the below two examples but i cant get them to work. example 1 if Not Dir("C:\DATA\YOURFILE.XLS") then 'the bugger isnot there! end if example2 Sub DoesFileExist() Set MyFile = CreateObject("Scripting.FileSystemObject") MsgBox MyFile.FileExists("c:\filename.xls") End Sub -- ceemo ------------------------------------------------------------------------ ceemo's Profile: http://www.excelforum.com/member.php...o&userid=10650 View this thread: http://www.excelforum.com/showthread...hreadid=400396 |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
Existing file ?
if Dir("C:\DATA\YOURFILE.XLS") ="" then
'the bugger isnot there! end if -- Regards, Tom Ogilvy "ceemo" wrote in message ... Im after some code that will check to see if a file exists in a specific location. Ive been given the below two examples but i cant get them to work. example 1 if Not Dir("C:\DATA\YOURFILE.XLS") then 'the bugger isnot there! end if example2 Sub DoesFileExist() Set MyFile = CreateObject("Scripting.FileSystemObject") MsgBox MyFile.FileExists("c:\filename.xls") End Sub -- ceemo ------------------------------------------------------------------------ ceemo's Profile: http://www.excelforum.com/member.php...o&userid=10650 View this thread: http://www.excelforum.com/showthread...hreadid=400396 |
#4
Posted to microsoft.public.excel.programming
|
|||
|
|||
Existing file ?
Good afternoon Ceemo My example works fine for me when called from a macro. Copy it int your VBE and run it. making sure that you don't have a file calle "c:\book1.xls" in that location. The result will be false. Chang line 3 to a file that does exist and run it again to get true. Sub DoesFileExist() Set MyFile = CreateObject("Scripting.FileSystemObject") MsgBox MyFile.FileExists("c:\book1.xls") End Sub HTH Dominic -- dominic ----------------------------------------------------------------------- dominicb's Profile: http://www.excelforum.com/member.php...fo&userid=1893 View this thread: http://www.excelforum.com/showthread.php?threadid=40039 |
#5
Posted to microsoft.public.excel.programming
|
|||
|
|||
Existing file ?
This works great but if i am pointing to th a: drive and there isnt floppy in it the macro will fall over. Is there anyway arond this? Sub test() Dim bGotIt As Boolean Dim sFile As String Dim sPath As String sFile = "Book1.xls" sPath = "c:\temp\" 'On Error Resume Next bGotIt = (LCase(sFile) = LCase(Dir(sPath & sFile))) 'On Error GoTo 0 MsgBox bGotIt End Su -- ceem ----------------------------------------------------------------------- ceemo's Profile: http://www.excelforum.com/member.php...fo&userid=1065 View this thread: http://www.excelforum.com/showthread.php?threadid=40039 |
#6
Posted to microsoft.public.excel.programming
|
|||
|
|||
Existing file ?
One way:
Option Explicit Sub test() Dim bGotIt As Boolean Dim sFile As String Dim sPath As String Dim testStr As String sFile = "Book1.xls" sPath = "a:\temp\" testStr = "" On Error Resume Next bGotIt = (Len(testStr) 0) On Error GoTo 0 MsgBox bGotIt End Sub ceemo wrote: This works great but if i am pointing to th a: drive and there isnt a floppy in it the macro will fall over. Is there anyway arond this? Sub test() Dim bGotIt As Boolean Dim sFile As String Dim sPath As String sFile = "Book1.xls" sPath = "c:\temp\" 'On Error Resume Next bGotIt = (LCase(sFile) = LCase(Dir(sPath & sFile))) 'On Error GoTo 0 MsgBox bGotIt End Sub -- ceemo ------------------------------------------------------------------------ ceemo's Profile: http://www.excelforum.com/member.php...o&userid=10650 View this thread: http://www.excelforum.com/showthread...hreadid=400396 -- Dave Peterson |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
User replaces existing file with blank during file open | Excel Discussion (Misc queries) | |||
Importing text file, only option to edit existing file | Excel Discussion (Misc queries) | |||
Creating a new excel file from an existing file using macro | Excel Programming | |||
can i save an existing .xls file as a .csv file using command line | Excel Discussion (Misc queries) | |||
get path - save new file - same sub-directory as existing file | Excel Programming |