Wrox Programmer Forums
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 8th, 2005, 12:03 PM
Authorized User
 
Join Date: Feb 2005
Posts: 85
Thanks: 0
Thanked 0 Times in 0 Posts
Default Get File path

Hi all,

I have code which searches for a file on the computer, and returns its path (the File Search property, and a API function)

However, this takes some time and I was wondering of there is another function which returns a file's path quickly.

I noticed that you can get the path of a workbook very fast, so I presume there is a funtion which can do the same to any kind of file.

thanks
-Max
 
Old March 8th, 2005, 03:16 PM
Authorized User
 
Join Date: Sep 2004
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Max -

Have you tried Application.GetOpenFilename and Application.Pathfinder?

Application.GetOpenFilename allows the user to click on the file he wants to open, similiarly to Excel->Open. It returns with full file name, including path. You can then use Application.Pathfinder to extract the file name only.

Kathy

 
Old March 8th, 2005, 04:39 PM
Authorized User
 
Join Date: Feb 2005
Posts: 85
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I know those options, except for application.pathfinder (this option is not available in VBA somehow)

I would like to quickly search for a file on a drive and get its path, without asking the user for input. It must be an automated search. I know a few ways, but Id like to hear other people use to search for files quickly

-max
 
Old April 6th, 2005, 12:52 PM
Registered User
 
Join Date: Mar 2005
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Max,

I don't know if this is any help but it seems reasonably fast to me.

Sub SearchForFiles()

  Dim i As Long
  Dim fs As FileSearch

  Set fs = Application.FileSearch
  With fs
    .NewSearch
    .LookIn = "C:\"
    .SearchSubFolders = True
    .Filename = "*.xla"
    .MatchTextExactly = False
    If .Execute() > 0 Then
      MsgBox "There were " & .FoundFiles.Count & " file(s) found."
      For i = 1 To .FoundFiles.Count
        ActiveSheet.Cells(i, 1).Value = .FoundFiles(i)
      Next i
    Else
      MsgBox "There were no files found."
    End If
  End With
  Set fs = Nothing

End Sub

Cheers
nic

 
Old April 6th, 2005, 01:19 PM
Registered User
 
Join Date: Mar 2005
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I just came across some of your other stuff relating to Application.FileSearch and its propensity to fail so I tried some quick experiments. This may be a red herring and you might of tried it but I found that I could get FileSearch to fail on a machine that it worked on previously if I enabled Indexing Service. If I then stop Indexing Service FileSearch works again. I don't know if this is repeatable on other machines but it seems reasonably consistent on my machine.

Cheers
nic






Similar Threads
Thread Thread Starter Forum Replies Last Post
Retrieve Active File Path francois.timms Excel VBA 3 March 24th, 2008 05:14 PM
how to call file using full path labby C# 2005 4 September 27th, 2006 11:25 PM
Knowing File Path x_ray VB.NET 2002/2003 Basics 8 February 15th, 2006 04:12 PM
Getting path of the file zorglub76 C# 4 December 23rd, 2005 04:25 AM
How to get the File Path DarthSith ASP.NET 1.0 and 1.1 Basics 2 October 29th, 2004 02:24 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.