View Single Post
 
Old 10-04-2017, 01:32 AM
slaycock slaycock is offline Windows 7 64bit Office 2013
Expert
 
Join Date: Sep 2013
Posts: 255
slaycock is on a distinguished road
Default

My code worked fine on my laptop using

SelectFileOpenDialog("Y:\Templates", "Hello World", "*.dotm")

Based on the structures you are using your app would seem to be Autocad(?) (There is lots of similar stuff if you google "GetOpenFileName Lib "comdlg32.dll""

I don't have Autocad so my examples are using MS Word as the VBA host.

When attempting to run your code it now gets stuck at

Code:
OFN.hwndOwner = GetActiveWindow()
as GetActiveWindow is another user defined function.

I tried to get around this as follows

Code:
OFN.hwndOwner = ActiveDocument.ActiveWindow.Hwnd ' GetActiveWindow()
and indeed the code runs. However using the inputs that work with my function do not produce anything i.e.

Code:
lReturn = GetOpenFileName(OFN)
just returns 0.

Your strategy of changing the 1 in

Code:
fd.Filters.Add "Files", strFilter, 1
was incorrect. If you want to understand why in your code place the cursor on the add and press F1. This should bring up the relevant help page (works for all VBA keywords and objects)

If the above doesn't work because you are in VBA for AutoCad then try looking at

http://analystcave.com/vba-applicati...g-select-file/

as a random example of a google search.

It may be that I can't resolve this problem to your using Autocad.

The code below does not select a filename using your code (with my OFN.hwndOwner fudge, but my code works fine on my pc. I've also added a few more file filters so you can see how they work *IF* the file dialog works correctly. The code below is stand-alone when run in word sp please try it in an Office application first and then in Autocad. In autocad you will need to remove my OFN.hwndOwner fudge.

Code:
Public Declare PtrSafe Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long


Public Type OPENFILENAME
    lStructSize As Long
    hwndOwner As Long
    hInstance As Long
    lpstrFilter As String
    lpstrCustomFilter As String
    nMaxCustFilter As Long
    nFilterIndex As Long
    lpstrFile As String
    nMaxFile As Long
    lpstrFileTitle As String
    nMaxFileTitle As Long
    lpstrInitialDir As String
    lpstrTitle As String
    flags As Long
    nFileOffset As Integer
    nFileExtension As Integer
    lpstrDefExt As String
    lCustData As Long
    lpfnHook As Long
    lpTemplateName As String
End Type

Sub test()

    Dim myString As String
    
    myString = SelectFileOpenDialog("Y:\Templates", "Hello World", "*.dotm")
    Debug.Print myString

    myString = SelectFileOpenDialog2("Y:\Templates", "Hello World", "*.dotm")
    Debug.Print myString
End Sub

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

    Dim strTemp As String
    Dim strTemp1 As String
    Dim pathStr As String
    Dim i As Long
    Dim n As Long
    Dim j As Long
    Dim OFN As OPENFILENAME
    Dim lReturn As Long
    Dim strFilterString As String
    Dim strFilename As String

    OFN.lStructSize = Len(OFN)
    strFilterString = Replace(strFilter, "|", Chr(0)) & Chr(0)
    OFN.lpstrFilter = strFilterString
    OFN.nFilterIndex = 1
    OFN.lpstrFile = String(257, 0)
    OFN.nMaxFile = Len(OFN.lpstrFile) - 1
    OFN.lpstrFileTitle = OFN.lpstrFile
    OFN.nMaxFileTitle = OFN.nMaxFile
    OFN.lpstrInitialDir = strInitDir
    OFN.lpstrTitle = strTitle
    OFN.hwndOwner = ActiveDocument.ActiveWindow.Hwnd ' GetActiveWindow()
    OFN.flags = 0

    lReturn = GetOpenFileName(OFN)

    If lReturn = 0 Then
        MsgBox "You didn't select any file"
        strFilename = ""
    Else
        strFilename = Trim$(OFN.lpstrFile) ' copy the filename
    End If
    SelectFileOpenDialog = strFilename

End Function


Public Function SelectFileOpenDialog2(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.Clear
    fd.Filters.Add "Word Files", "*.doc;*.dot;*.docx;*.dotx;*.dotm"
    fd.Filters.Add "Text/CSV files", "*.txt;*.csv"
    fd.Filters.Add "Excel files", "*.xlsx;*.xls;*.xlsm"
    fd.Filters.Add "All files", "*.*"
    fd.Filters.Add "Your file type description", strFilter
    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
    SelectFileOpenDialog2 = CStr(strFilename)

End Function
Reply With Quote