Select file or folder names in Word
2000-02-04 Files & Folders 0 341
Word has not got the method GetOpenFileName that can be used to let a user select a file name or a folder. You can use the user defined function below:
Function WordApplicationGetOpenFileName(FileFilter As String, _
ReturnPath As Boolean, ReturnFile As Boolean) As String
' returns the folder and/or filename to a single user selected file
Dim strFileName As String, strPathName As String
If Not ReturnPath And Not ReturnFile Then Exit Function
If FileFilter = "" Then FileFilter = "*.*"
With Application.Dialogs(wdDialogFileOpen)
.Name = FileFilter
On Error GoTo MultipleFilesSelected
If .Display = -1 Then
strFileName = .Name
End If
On Error GoTo 0
End With
On Error GoTo 0
' remove any "-characters
If InStr(1, strFileName, " ", vbTextCompare) > 0 Then
strFileName = Mid$(strFileName, 2, Len(strFileName) - 2)
End If
If ReturnPath Then
strPathName = CurDir & Application.PathSeparator
Else
strPathName = ""
End If
If Not ReturnFile Then strFileName = ""
WordApplicationGetOpenFileName = strPathName & strFileName
MultipleFilesSelected:
End Function
Use the function like this to return a complete folder and file name:FullFileName = WordApplicationGetOpenFileName("*.doc", True, True)
Use the function like this to return the file name only:
FileNameOnly = WordApplicationGetOpenFileName("*.doc", False, True)
Use the function like this to return the folder name only:
FolderNameOnly = WordApplicationGetOpenFileName("*.*", True, False)