PDA

View Full Version : Get FullPath of file opened using the UI File Open dialog



gmaxey
10-20-2015, 04:22 AM
Sub ScratchMacro()
'Due to a document managment system, I need to set a document object variable based on the user's selection
'from the file open dialog.
Dim oDoc As Word.Document
If Application.Dialogs(wdDialogFileOpen).Show = -1 Then
'It seems the new document is always indexed as 1 but I'm not sure.
Set oDoc = Documents(1)
'What I would really like is a way to just display the File Open dialog and get the FullName (Path and File name) of the file selected
'validate it and then explicitly Set oDoc = Documents.Open(Path and File name).
'Any ideas?
End If
MsgBox oDoc.FullName
lbl_Exit:
Exit Sub
End Sub


Thanks!

gmayor
10-20-2015, 04:43 AM
Why not use the following? If you call it with strTitle = "Open" it looks exactly the same as the FileOpen dialog then


strPath = BrowseForFolder("Open")
If Not strPath = "" then
Set oDoc = Documents.Open(strpath)
Else
Exit Sub
End If


Function BrowseForFile(Optional strTitle As String, Optional bExcel As Boolean) As String
'Graham Mayor
'strTitle is the title of the dialog box
'Set bExcel value to True to filter the dialog to show Excel files
'The default is to show Word files
Dim fDialog As FileDialog
On Error GoTo err_Handler
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With fDialog
.Title = strTitle
.AllowMultiSelect = False
.Filters.Clear
If bExcel Then
.Filters.Add "Excel workbooks", "*.xls,*.xlsx,*.xlsm"
Else
.Filters.Add "Word documents", "*.doc,*.docx,*.docm"
End If
.InitialView = msoFileDialogViewList
If .Show <> -1 Then GoTo err_Handler:
BrowseForFile = fDialog.SelectedItems.Item(1)
End With
lbl_Exit:
Exit Function
err_Handler:
BrowseForFile = vbNullString
Resume lbl_Exit
End Function


This and related functions can be found on my web site at http://www.gmayor.com/useful_vba_functions.htm

gmaxey
10-20-2015, 08:35 AM
Graham,

The lady says that their document management system is triggered by the Word FileOpen dialog. That is the only reason that I know.