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 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


 
I'm not efficient at writing code, but I took a stab at this. Other than the error handling not being quite right, it seems to do the job.
Code:
Sub choosefolder()
Dim FldrName As String
FldrName = ""
On Error GoTo ErrorHandler
Do While FldrName = ""
AskUser:
' Ask User for the folder name; default number = "" (nothing)
FldrName = InputBox("Which folder do you want displayed?", _
"Folder Name", "")
ChangeFileOpenDirectory "m:\legal\" & FldrName
Dialogs(wdDialogFileOpen).Show
'Check to see if there is any entry made to input box, or if
    'cancel button is pressed. If no entry made then exit sub.
Dim x As Boolean
x = Application.Dialogs(xlDialogOpen).Show
If x = False Then Exit Sub
Exit Do
Loop

ErrorHandler:
Dim Msg
Msg = "Invalid Folder Name"
End Sub
 
Code:
ErrorHandler:
Dim Msg
Msg = "Invalid Folder Name"
End Sub
Msg is declared as a Variant, set as a String....and never used. Any Error would exit the Sub, without any message displayed. The user enters "yadda" into the Inputbox. There is an error. The code jumps to ErrorHandler which sets the string and exits without doing anything else. The ErrorHandler does nothing.
Code:
x = Application.Dialogs(xlDialogOpen).Show
will fail, as the OP is working in Word (the subject - "Word 2003 macro" I assume is correct). So xlDialogOpen will fail...as that is not a Word dialog. It is an Excel dialog. However, even with the proper dialog,
Code:
    Dim x As Boolean
    x = Application.Dialogs(wdDialogFileOpen).Show
This code displays the dialog again. In other words, if the user enters a valid folder name (no error in the first part), selects a file...the code will open the file...and display the dialog again. Regardless. They cancel, it displays again. They pick something, it displays again.

MVDRUM, I am not sure what you mean by a "search field".
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.
If I understand it correctly, dcompto is very close to what you may want. Although there is no need for a Do Loop, as invalid input is handled by the error handler - its purpose.
Code:
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
Generally speaking, if you change a system value (in this cases the folder Word will use for File > Open) for your own purposes, it is good practice to return things to their original values. This is done with CleanUp, which puts the FileOpen value back to whatever it was at the start.

I am not sure what is required for other error trapping. What do you actually want to do if the user Cancels? The code above simply exits.

faq219-2884

Gerry
My paintings and sculpture
 
Hey all, thank you very much for the responses
Fumei's code 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 !! :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top