View Single Post
 
Old 10-02-2017, 09:55 PM
slaycock slaycock is offline Windows 7 64bit Office 2013
Expert
 
Join Date: Sep 2013
Posts: 255
slaycock is on a distinguished road
Default

We need the definition of the OPENFILENAME structure/object to be able to run the SelectFileOpenDialog function.

In the meantime the function appears to just return a filename. The OPENFILENAME requirement may be a red herring because whilst it is used in the function it is a local variable and so the only product it returns is the field/property OFN.lpstrFile.

Word has moved on since your macro was writen and consequently it is possible to use a much simpler version to return a filename for subsequent use.

Code:
Public Function SelectFileOpenDialog(strInitDir As String, strTitle As String, strFilter As String) As String
    Dim fd As FileDialog
    Dim lReturn As Long
    Dim strFilename As String
    Set fd = Application.FileDialog(msoFileDialogFilePicker)

    fd.AllowMultiSelect = False
    ' str filter should contain a comma seperated list of filters of the form
    ' *.<application extention> e.g. *.docx or *.xlsx etc.
    fd.Filters.Add "Files", strFilter, 1
    fd.Title = strTitle
    fd.InitialFileName = strInitDir
    fd.InitialView = msoFileDialogViewDetails
    lReturn = fd.Show

    If lReturn = 0 Then
        MsgBox "You didn't select any file"
        strFilename = ""
    Else
        strFilename = Trim$(fd.SelectedItems(1)) ' copy the filename - The 1 might need to be a zero
    End If
    SelectFileOpenDialog = strFilename

End Function
The code above return a path enabled filename

e.g. C:\...........\<filename you selected>

and so may need adjusting depending on how the returned filename is used.

Try renaming your code to

Public Function SelectFileOpenDialog1(strInitDir As String, strTitle As String, strFilter As String) As String

paste in the code above and see what happens when you try to select a file.
Reply With Quote