Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word 2003 "search for folder name" macro 1

Status
Not open for further replies.

MVDRUM

IS-IT--Management
Oct 10, 2007
11
I'm sure something like this has been covered but i can seem to locate it.



I would like to create a macro that will search a specific mapped drive for the correct folder name and then display the contents of that folder for the user to then select the appropriate document to open...the user would enter in the directory name such as "56608".



Each of these folders are numbers and are located in a parent folder called "legal" located here..



m:\legal\



how do i create a simple macro that when selected by the users it brings up a search field for them to enter in the appropriate number (which is actually the folder name) and the contents of that number (folder) is displayed.

There are currently over 6,000 numbered folders.



Thanks a bunch in advance

MV
 
Maybe something like this:
Code:
mypath="x:\" 'or whatever the mapped drive is
mypath=mypath & inputbox("enter folder name:")
ChangeFileOpenDirectory mypath
Dialogs(wdDialogFileOpen).Show

_________________
Bob Rashkin
 
Hey Bong...Thanks for the reply
That seems to be working...however only 50% of the time...its hit and miss... the other time it reverts back to the local my documents folder...
I think this must be a VISTA related issue?

One more question...should the user enter the incorect directory name it comes up with a runtime error...what code would i need to add so that it would ask if they would like to create the directory should it not find the one they have entered and then create the directory for them?
 
You may use this function:
Code:
Function PickFolder(strStartDir As Variant) As String
Dim SA As Object, f As Object
Set SA = CreateObject("Shell.Application")
Set f = SA.BrowseForFolder(0, "Choose a folder", 16 + 32 + 64, strStartDir)
If (Not f Is Nothing) Then
  PickFolder = f.Items.Item.path
End If
Set f = Nothing
Set SA = Nothing
End Function

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi MV,

If you'd prefer the user to navigate to the folder, so as to avoid typos, you could use something like:
Code:
Sub Test()
Dim MyFolder As String
MyFolder = GetFolder(Title:="Find a Folder", RootFolder:=&H11)
If MyFolder = "" Then
    MsgBox "No Folder Selected"
Else
    MsgBox MyFolder
End If
End Sub

Function GetFolder(Optional Title As String, Optional RootFolder As Variant) As String
On Error Resume Next
GetFolder = CreateObject("Shell.Application").BrowseForFolder(0, Title, 0, RootFolder).Items.Item.Path
End Function
Cheers

[MS MVP - Word]
 
Thanks for the options guys, However the only one that works so far is the 1st one that Bong posted.
Im way in over my head here as im not a programmer at all

Here is what is in the macro:

Sub FIRM()
'
' FIRM Macro
'
'
mypath = "M:\Matters\"
mypath = mypath & InputBox("Enter Matters Number:")
ChangeFileOpenDirectory mypath
Dialogs(wdDialogFileOpen).Show

End Sub
 
Hi MV,

A little tweaking gives you the ability to navigate, then have the app display the File|Open dialogue box for that folder:
Code:
Sub Firm()
Dim MyFolder As String
MyFolder = GetFolder(Title:="Find a Folder", RootFolder:=&H11)
If MyFolder <> "" Then
    ChangeFileOpenDirectory MyFolder
    Dialogs(wdDialogFileOpen).Show
Else
    MsgBox "No Folder Selected"
End If
End Sub

Function GetFolder(Optional Title As String, Optional RootFolder As Variant) As String
On Error Resume Next
GetFolder = CreateObject("Shell.Application").BrowseForFolder(0, Title, 0, RootFolder).Items.Item.Path
End Function
Cheers

[MS MVP - Word]
 
Thanks Macropod,

Im not sure how to use your code with the existing code:
Where do i add your code....?? do i append it on the end of the following?
Thanks again

Sub FIRM()
'
' FIRM Macro
'
'
mypath = "M:\Matters\"
mypath = mypath & InputBox("Enter Matters Number:")
ChangeFileOpenDirectory mypath
Dialogs(wdDialogFileOpen).Show

End Sub
 
Hi MV,
Im not sure how to use your code with the existing code:
Where do i add your code....??
you don't! It's a complete replacement - simply substitute my code for your existing code.

Cheers

[MS MVP - Word]
 
Hey all, thank you very much for the responses
I have this listed in another area as well...guess it should only be here

Fumei has given me the following code and it works great...

If i may ask one more thing...If the folder number the user
enters is NOT found is there a way to ask the user something like -

"Folder not found. Would you like to create it now ?"
And have an option of "yes" and the user enters the new folder name and is then taken to that new folder.
And an option of "No" and the user is prompted for teh corect folder naem.

Thanks again all !! :)


Sub choosefolder()
Dim FldrName As String
Dim myCurDir As String
myCurDir = Application.Options.DefaultFilePath(wdDocumentsPath)

On Error GoTo ErrorHandler
AskUser:
' Ask User for the folder name
FldrName = InputBox("Which folder do you want displayed?", _
"Folder Name", "")
ChangeFileOpenDirectory "m:\legal\" & FldrName
Dialogs(wdDialogFileOpen).Show
GoTo CleanUp:

ErrorHandler:
MsgBox "Invalid Folder Name"
GoTo AskUser:
CleanUp:
ChangeFileOpenDirectory myCurDir
End Sub
 
There may be better ways to do this but here's what I would do:

between
Code:
 FldrName = InputBox("Which folder do you want displayed?", "Folder Name", "")
and
Code:
ChangeFileOpenDirectory "m:\legal\" & FldrName
you check to see if the folder exists.
Code:
Set fs = CreateObject("Scripting.FileSystemObject")
strPath="m:\legal\" & FldrName
if not fs.folderexists(strPath) then
  bMkDir = msgbox("Folder does not exist ...etc",4)
  if bMkDir = 6 then mkdir(strPath)
else 
  [red]go back to the input box[/red]
end if

_________________
Bob Rashkin
 
Hey Bong thanks man...that worked great !
Thanks to all who contributed !! very much apreciated !
 
You can do the new folder thing without FSO - for which of course you would need a reference.
Code:
ErrorHandler:
Dim response
response = MsgBox("Invalid folder name.  Do you wish to create it?" & _
   vbCrLf & "Click Yes to create the folder." & _
   vbCrLf & "Click No to try a different name.", _
   vbYesNo)
If response = vbYes Then
   MkDir ("m:\legal\" & FldrName)
   ChangeFileOpenDirectory "m:\legal\" & FldrName
   Dialogs(wdDialogFileOpen).Show
   GoTo CleanUp:
Else
   GoTo AskUser:
End If
Assuming that after the new folder is created you actually want to go there. If not, then change the code appropriately.

faq219-2884

Gerry
My paintings and sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top