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