Question : OPENFILENAME truncating the file name

I am using the OPENFILENAME to open a common dialog box and, I am supplying the suggested name of the file and going to an initdir.  I want my users to find the dir they want, and when the dialog screen is closed, I want to take the value of the directory they chose, along with the file name (whether it be the one I suggested, or a new one) and use it to create an Excel file.

All is well, EXCEPT that the file name is being truncated.  I am looking for the full name (path as chosen PLUS the new file name (though it is, by default using the one I place in there).  It seems that it is returning a value based on the number of characters my "suggested file", or the value passed through below (using Case 4 for this) via strFileName, not allowing the total length I should get with the Directory + File name.  It ends up truncating the end of the full file name.

Any ideas?

laura
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
Dim strTemp, strTemp1, pathStr As String
Dim i, n, j As Long
Dim OpenFile As OPENFILENAME
Dim lReturn As Long
Dim sFilter As String
 
 
 
OpenFile.lStructSize = Len(OpenFile)
 
 
OpenFile.nFilterIndex = 1
OpenFile.lpstrFile = String(257, 0)
OpenFile.nMaxFile = Len(OpenFile.lpstrFile) - 1
OpenFile.nMaxFileTitle = OpenFile.nMaxFile
 
 
'If strFileName = "" Then
Select Case intType
    Case 1
        '''powerpoint templates
                OpenFile.lpstrTitle = "Browse for Powerpoint Templates"
                If Not CkForDirectory(strDir) Then
                ''doesn't exist, let's make it
                    MkDir (strDir)
                End If
                OpenFile.lpstrInitialDir = strDir
            
                OpenFile.lpstrFileTitle = OpenFile.lpstrFile
                    sFilter = "Powerpoint (*.ppt)" & Chr(0) & "*.ppt"
                OpenFile.lpstrFilter = sFilter
    Case 2
         '''powerpoint files
                OpenFile.lpstrTitle = "Browse for Powerpoint Files"
                OpenFile.lpstrInitialDir = strDir
                OpenFile.lpstrFileTitle = OpenFile.lpstrFile
                sFilter = "Powerpoint (*.ppt)" & Chr(0) & "*.ppt"
                  'sFilter = "Powerpoint (*.ppt)" & Chr(0) & "*.ppt"
                OpenFile.lpstrFilter = sFilter
    Case 3
     '''excel files
            OpenFile.lpstrTitle = "Browse for Excel Files"
            OpenFile.lpstrInitialDir = strDir
            OpenFile.lpstrFileTitle = OpenFile.lpstrFile
               sFilter = "Excel (*.xls)" & Chr(0) & "*.xls"
            OpenFile.lpstrFilter = sFilter
        Case 4
            '''excel projection files
            OpenFile.lpstrTitle = "Save the Projection file"
           ' OpenFile.lStructSize = Len(strFileName)
            OpenFile.lpstrFile = strFileName
            OpenFile.lpstrInitialDir = strDir
            OpenFile.lpstrFileTitle = OpenFile.lpstrFile
               sFilter = "Excel (*.xls)" & Chr(0) & "*.xls"
            OpenFile.lpstrFilter = sFilter
End Select
    
 
OpenFile.flags = 0
 
lReturn = GetOpenFileName(OpenFile)
 
If lReturn = 0 Then
 
Else
 
 
FindMyFiles = OpenFile.lpstrFile
 
 
End If
 
End Function

Answer : OPENFILENAME truncating the file name

LAura,

This is what I use and have it in a Module,

My input is set for excel files but   if I have
GetFileName("mdb")    then it looks for MDB files

Thers a lot of stuff commented out in this module sinc I don't need it  but left it for future
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
Option Compare Database
 
' This code is licensed according to the terms and conditions listed here.
 
' Declarations and such needed for the example:
' (Copy them to the (declarations) section of a module.)
Public Type OPENFILENAME
    lStructSize As Long
    hwndOwner As Long
    hInstance As Long
    lpstrFilter As String
    lpstrCustomFilter As String
    nMaxCustomFilter 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
Public Const OFN_FILEMUSTEXIST = &H1000
Public Const OFN_HIDEREADONLY = &H4
Public Const OFN_PATHMUSTEXIST = &H800
Public Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (lpofn _
    As OPENFILENAME) As Long
Public Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" _
  (pOpenfilename As OPENFILENAME) As Long
 
 
'ADD LISTBOX HEIGHT CHANGER
'Public Declare Function SendMessage Lib "comdlg32.dll" Alias "SendMessageA" (ByVal _
'    hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
'     lParam As Any) As Long
'Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal _
 '   hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
  '    lParam As Any) As Long
'Const LB_SETITEMHEIGHT = &H1A0
'Const CB_SETITEMHEIGHT = &H153
 
 
 
' Set the height in pixels of each entry in a ListBox or ComboBox control
 
'Sub SetListItemHeight(ctrl As Control, ByVal newHeight As Long)
 
 
 '   Dim uMsg As Long
 '   If TypeOf ctrl Is ListBox Then
 '       uMsg = LB_SETITEMHEIGHT
 '   ElseIf TypeOf ctrl Is ComboBox Then
 '       uMsg = CB_SETITEMHEIGHT
 '   Else
 '       Exit Sub
 '   End If
 '   ' (only the low-order word of lParam can be used.)
  '  SendMessage ctrl.hwnd, uMsg, 0, ByVal CLng(newHeight And &HFFFF&)
  '  ' It is necessary to manually refresh the control.
  '  ctrl.Refresh
'End Sub
 
 
Function GetFileName(Optional strIntype As String, Optional DBProjectName)
 
 
'Set Variables for Document Name
 
    Dim filebox As OPENFILENAME  ' open file dialog structure
    Dim fname As String          ' filename the user selected
    Dim result As Long           ' result of opening the dialog
    Dim SaveAsName As String       ' file to save in the dialog
    Dim ExtName As String
 
 
'On Error GoTo Err_Command5_Click
'Make Reference to Microsoft Word XX object Library
 
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Set db = CurrentDb
    
     
 
'Get document Name
    ' Configure how the dialog box will look
    With filebox
        ' Size of the structure.
        .lStructSize = Len(filebox)
        ' Handle to window opening the dialog.
            .hwndOwner = hwnd    'Me.Hwnd
        ' Handle to calling instance (not needed).
        .hInstance = 0
        ' File filters to make available: Text Files and All Files
        .lpstrFilter = "Excel Files (*.xls)" & vbNullChar & "*.xls" & vbNullChar & vbNullChar
        If strIntype = "mdb" Then
         .lpstrFilter = "Access Files (*.mdb)" & vbNullChar & DBProjectName & vbNullChar & vbNullChar
        End If
        
        
        .nMaxCustomFilter = 0
        ' Default filter is the first one (Text Files, in this case).
        .nFilterIndex = 1
        ' No default filename.  Also make room for received
        ' path and filename of the user's selection.
        .lpstrFile = Space(256) & vbNullChar
        .nMaxFile = Len(.lpstrFile)
        ' Make room for filename of the user's selection.
        .lpstrFileTitle = Space(256) & vbNullChar
        .nMaxFileTitle = Len(.lpstrFileTitle)
        ' Initial directory is C:\.
        .lpstrInitialDir = "C:\" & vbNullChar
        ' Title of file dialog.
        .lpstrTitle = "Select the File to be searched" & vbNullChar
        ' The path and file must exist; hide the read-only box.
        .flags = OFN_PATHMUSTEXIST Or OFN_FILEMUSTEXIST Or OFN_HIDEREADONLY
        ' The rest of the options aren't needed.
        .nFileOffset = 0
        .nFileExtension = 0
        '.lpstrDefExt is ignored -- unused string
        .lCustData = 0
        .lpfnHook = 0
        '.lpTemplateName is ignored -- unused string
    End With
 
    ' Display the dialog box.
    result = GetOpenFileName(filebox)
    
    
    
    If result <> 0 Then
        ' Remove null space from the file name.
        fname = Left(filebox.lpstrFile, InStr(filebox.lpstrFile, vbNullChar) - 1)
        ExtName = right(fname, 3)
        fnameext = ExtName
        'Debug.Print "The selected file: "; fname
    End If
    GetFileName = fname
    
    'ImportFileNameField = fname
    
   ' With filebox
   ' .lpstrInitialDir = "C:\temp\" & vbNullChar
   ' .lpstrFile = "C:\Temp\WordToAccess.txt" & vbNullChar
   ' .lpstrFilter = "Text (*.txt)" & vbNullChar & "*.txt" & vbNullChar & vbNullChar
   ' End With
    
    'result = GetSaveFileName(filebox)
    'If result <> 0 Then
    '' Remove null space from the file name.
    'SaveAsName = Left(filebox.lpstrFile, InStr(filebox.lpstrFile, vbNullChar) - 1)
    'ExtName = Right(fname, 3)
    'fnameext = ExtName
    'Debug.Print "The selected file: "; fname
    'End If
End Function
Random Solutions  
 
programming4us programming4us