Finding a file's path from File Open dialog

R

Richard Finn

Hi,
I'm using the wdDialogFileOpen dialog to select a file and need to extract the selected file's name, extension and path. The name and extension worked OK (though there may be an better way of doing it, suggestions welcome) but the path generates a run-time error.

Specific questions are :-
1 How do I get the path of the selected file?
2 If the user cancels the dialog box, how can I quit from the macro and close the dialog box without quitting from Word?
3 Is there a tidier way of getting the main filename and extension?

Thanks for any suggestions,

Richard


Private Sub cmdFirst_Click()

Dim dlg As Dialog
Dim Dot As Integer
Dim Reply As Long

' Select first file with dialog box
Set dlg = Dialogs(wdDialogFileOpen)
With dlg
.Name = "*.*"
Reply = .Display ' Don't open the file, just select it
If Reply <> -1 Then ' Did user click on Cancel?
Application.Quit wdDoNotSaveChanges ' If so, quit from Word
End If
txtDir.Text = .Path ' THIS GIVES AN ERROR, HOW DO I GET THE PATH?
Dot = InStr(.Name, ".")
txtFileName.Text = Left(.Name, Dot - 1)
txtFirst.Text = Mid(.Name, Dot + 1)
End With
End Sub
 
J

Jay Freedman

Hi Richard,

The WordBasic.FileNameInfo$() function will do this for you, as
described in
http://word.mvps.org/FAQs/MacrosVBA/WordBasicCommands.htm. The example
there shows how to get the full path and filename in one string.

If you still want to separate the parts, make two calls to the
function with the FileType parameter set successively to 5 (the path
without the filename) and then either 3 or 4 (the filename with or
without the extension). To get the extension separately, you can do
what you're doing now, or get the difference between the strings with
and without extension.
 
J

Jonathan West

Hi Richard

1. This article contains the means of getting a full path. Look at thr part
of the article that deals with the FileNameInfo$ function

Useful WordBasic commands that have no VBA equivalent
http://www.word.mvps.org/FAQs/MacrosVBA/WordBasicCommands.htm

2. The value returned by the Display mthod tells you which button was used
to close the dialog. See the Help entry for the Display method. If you want
to just close the userform without quitting the whole of Word, then use the
command "Unload Me" instead of "Application.Quit"

3. The FileNameInfo function also allows you to get the filename without
extension. Alternatively, you should use InstrRev rather than Instr if you
are running Word 2000 or later, in order to get the position of the last
period in the filename. Your code will return incorrect information if there
is a period in the filename apart from the one separating the filename from
the extension.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup


Hi,
I'm using the wdDialogFileOpen dialog to select a file and need to extract
the selected file's name, extension and path. The name and extension worked
OK (though there may be an better way of doing it, suggestions welcome) but
the path generates a run-time error.

Specific questions are :-
1 How do I get the path of the selected file?
2 If the user cancels the dialog box, how can I quit from the macro and
close the dialog box without quitting from Word?
3 Is there a tidier way of getting the main filename and extension?

Thanks for any suggestions,

Richard


Private Sub cmdFirst_Click()

Dim dlg As Dialog
Dim Dot As Integer
Dim Reply As Long

' Select first file with dialog box
Set dlg = Dialogs(wdDialogFileOpen)
With dlg
.Name = "*.*"
Reply = .Display ' Don't open the file, just select it
If Reply <> -1 Then ' Did user click on Cancel?
Application.Quit wdDoNotSaveChanges ' If so, quit from Word
End If
txtDir.Text = .Path ' THIS GIVES AN ERROR, HOW DO I GET THE PATH?
Dot = InStr(.Name, ".")
txtFileName.Text = Left(.Name, Dot - 1)
txtFirst.Text = Mid(.Name, Dot + 1)
End With
End Sub
 
R

Richard Finn

Many thanks to Jonathan and Jay for their detailed replies. I'm sure I'd never have found out about functions that WordBasic supports and VBA doesn't with their help.

Cheers

Richard
Hi,
I'm using the wdDialogFileOpen dialog to select a file and need to extract the selected file's name, extension and path. The name and extension worked OK (though there may be an better way of doing it, suggestions welcome) but the path generates a run-time error.

Specific questions are :-
1 How do I get the path of the selected file?
2 If the user cancels the dialog box, how can I quit from the macro and close the dialog box without quitting from Word?
3 Is there a tidier way of getting the main filename and extension?

Thanks for any suggestions,

Richard


Private Sub cmdFirst_Click()

Dim dlg As Dialog
Dim Dot As Integer
Dim Reply As Long

' Select first file with dialog box
Set dlg = Dialogs(wdDialogFileOpen)
With dlg
.Name = "*.*"
Reply = .Display ' Don't open the file, just select it
If Reply <> -1 Then ' Did user click on Cancel?
Application.Quit wdDoNotSaveChanges ' If so, quit from Word
End If
txtDir.Text = .Path ' THIS GIVES AN ERROR, HOW DO I GET THE PATH?
Dot = InStr(.Name, ".")
txtFileName.Text = Left(.Name, Dot - 1)
txtFirst.Text = Mid(.Name, Dot + 1)
End With
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top