Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Searching for a file

I need a macro to look for a file name (the name of which will be contained
in a cell in the worksheet) in the default folder and if it doesn't exist
show a message and if it does then continue with the other steps in the macro.

If anyone can help it would be greatly appreciated.... Mark.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 400
Default Searching for a file

This API returns true is the file exists:

Declare Function PathFileExists Lib "shlwapi.dll" Alias "PathFileExistsA"
(ByVal pszPath As String) As Long

if PathFileExists(yourfilename) then ' do this'

You can construct 'youfilename' as you wish.

"Mark McW" wrote:

I need a macro to look for a file name (the name of which will be contained
in a cell in the worksheet) in the default folder and if it doesn't exist
show a message and if it does then continue with the other steps in the macro.

If anyone can help it would be greatly appreciated.... Mark.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default Searching for a file

Thanks for the help. However, do you mean that "(your filename)" is the only
variable in the macro code and is it supposed to enclosed in brackets?

Currently it is still erroring.

"AA2e72E" wrote:

This API returns true is the file exists:

Declare Function PathFileExists Lib "shlwapi.dll" Alias "PathFileExistsA"
(ByVal pszPath As String) As Long

if PathFileExists(yourfilename) then ' do this'

You can construct 'youfilename' as you wish.

"Mark McW" wrote:

I need a macro to look for a file name (the name of which will be contained
in a cell in the worksheet) in the default folder and if it doesn't exist
show a message and if it does then continue with the other steps in the macro.

If anyone can help it would be greatly appreciated.... Mark.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 400
Default Searching for a file

Yes the argument pszPath (can be a filename or a path) should be in round
brackets (i.e. to force the return of an explicit value i.e so as not to mask
the return value).

If A1 contains your file name, say, MyFile.XLS AND you want to search in the
path where Excel stores its workbook, you would call

If PathFileExists(application.defaultfilepath & "\" & range("A1").Value)
Then 'do this .. whatever you want

If A1 contains the fully qualified name of a file, e.g c:\my path\myfile.xls
you would do

If PathFileExists(range("A1").Value) Then 'do this .. whatever you want

If A1 contains a file name only (no .XLS) you want to search in c:\my alt
path, you would do

If PathFileExists("c:\my alt path" & "\" & range("A1").Value & ".XLS") Then
'do this ..

I.E the argument is the fully qualified name of the file you want to search
for.
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default Searching for a file

Thanks, but the macro is still erroring and highlighting the PathFileExists
portion. The code is as below. Can't figure out what I am doing wrong.

Sheet6.Select
spath = ActiveWorkbook.Path
If Right(spath, 1) < "\" Then spath = spath & "\"
If PathFileExists(spath & Sheet6.Range("u61").Value) Then MsgBox "File exists"
Exit Sub
Else

"AA2e72E" wrote:

Yes the argument pszPath (can be a filename or a path) should be in round
brackets (i.e. to force the return of an explicit value i.e so as not to mask
the return value).

If A1 contains your file name, say, MyFile.XLS AND you want to search in the
path where Excel stores its workbook, you would call

If PathFileExists(application.defaultfilepath & "\" & range("A1").Value)
Then 'do this .. whatever you want

If A1 contains the fully qualified name of a file, e.g c:\my path\myfile.xls
you would do

If PathFileExists(range("A1").Value) Then 'do this .. whatever you want

If A1 contains a file name only (no .XLS) you want to search in c:\my alt
path, you would do

If PathFileExists("c:\my alt path" & "\" & range("A1").Value & ".XLS") Then
'do this ..

I.E the argument is the fully qualified name of the file you want to search
for.



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 400
Default Searching for a file

I think you do have not included the API declaration in the declaration
section. Try another approach

Sub aa()
spath = ActiveWorkbook.Path
If Right(spath, 1) < "\" Then spath = spath & "\"
yourfilename = spath & Sheet6.Range("u61").Value
Select Case Len(Dir(yourfilename))
Case Is = 0
'file does not exist
Case Else
'file exists
End Select
End Sub

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default Searching for a file

Thanks very much, appreciate the help. That works fine....... Mark.

Do you also know a way that if you have a link established to another file,
how you can change it (in a macro). Again, by using a file name that is
contained in a cell?

"AA2e72E" wrote:

I think you do have not included the API declaration in the declaration
section. Try another approach

Sub aa()
spath = ActiveWorkbook.Path
If Right(spath, 1) < "\" Then spath = spath & "\"
yourfilename = spath & Sheet6.Range("u61").Value
Select Case Len(Dir(yourfilename))
Case Is = 0
'file does not exist
Case Else
'file exists
End Select
End Sub

  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Searching for a file

Look in Excel VBA help at the ChangeLink command.

--
Regards,
Tom Ogilvy


"Mark McW" wrote:

Thanks very much, appreciate the help. That works fine....... Mark.

Do you also know a way that if you have a link established to another file,
how you can change it (in a macro). Again, by using a file name that is
contained in a cell?

"AA2e72E" wrote:

I think you do have not included the API declaration in the declaration
section. Try another approach

Sub aa()
spath = ActiveWorkbook.Path
If Right(spath, 1) < "\" Then spath = spath & "\"
yourfilename = spath & Sheet6.Range("u61").Value
Select Case Len(Dir(yourfilename))
Case Is = 0
'file does not exist
Case Else
'file exists
End Select
End Sub

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
searching a text file and importing selected data brian Excel Discussion (Misc queries) 0 October 30th 07 08:44 PM
Searching for and Pasting Data to a new file Elaine Excel Programming 2 January 10th 06 07:15 PM
Recursive Function + File searching to return path ph8[_6_] Excel Programming 24 October 14th 05 08:45 AM
Searching a txt file for splitting into different worksheets Randy Reese Excel Programming 1 January 30th 04 09:20 PM
File Searching Lawson Excel Programming 1 January 30th 04 03:13 AM


All times are GMT +1. The time now is 12:46 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"