Consulting

Results 1 to 3 of 3

Thread: Get FullPath of file opened using the UI File Open dialog

  1. #1
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location

    Get FullPath of file opened using the UI File Open dialog

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

    Visit my website: http://gregmaxey.com

  2. #2
    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
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    Graham,

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

    Visit my website: http://gregmaxey.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •