SaveAs with default filename

R

Rudy Welvaert

How can I not add a default path, but also a default
filename in the folowing code...

The user should NOT be able to choose the filename, but he
can choose the folder where to save it. So the name of the
file should be visible already in the dialogbox of the
GetSaveFileName function. As it is now, the filename box
is blank.

Private Declare Function GetSaveFileName
Lib "comdlg32.dll" Alias _
"GetSaveFileNameA" (pOpenfilename As OpenFileName)
As Long

Private 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

Public Function ChooseFile(strTitle As String, strFilter
As String, ByVal strInitDir As String) As String
Dim ofnOpenFile As OpenFileName
Dim lonReturn As Long
Dim strFilter2 As String

On Error GoTo Err_ChooseFile

ofnOpenFile.lStructSize = Len(ofnOpenFile)
ofnOpenFile.hwndOwner = 0
ofnOpenFile.hInstance = 0
strFilter2 = strFilter '"Excel Files (*.xls)" & Chr(0)
& "*.XLS" & Chr(0)
ofnOpenFile.lpstrFilter = strFilter2
ofnOpenFile.nFilterIndex = 1
ofnOpenFile.lpstrFile = String(257, 0)
ofnOpenFile.nMaxFile = Len(ofnOpenFile.lpstrFile) - 1
ofnOpenFile.lpstrFileTitle = ofnOpenFile.lpstrFile
ofnOpenFile.nMaxFileTitle = ofnOpenFile.nMaxFile
ofnOpenFile.lpstrInitialDir = strInitDir
ofnOpenFile.lpstrTitle = strTitle
ofnOpenFile.flags = 0
lonReturn = GetSaveFileName(ofnOpenFile)
If lonReturn = 0 Then
ChooseFile= "-"
Else
ChooseFile= Left(Trim(ofnOpenFile.lpstrFile), InStr
(ofnOpenFile.lpstrFile, Chr(0)) - 1)
End If

Exit_ChooseFile:
Exit Function

Err_ChooseFile:
ChooseFile= "-"
Resume Exit_ChooseFile
End Function

....
strDefaultPath = "C:\"
strSavePath = ChooseFile("Where do you want to save your
Excel file?","Excel Files (*.xls)" & Chr(0) & "*.xls" & Chr
(0) & Chr(0), strDefaultPath, False)

....
 
R

Rudy Welvaert

Thanks Allen,

This code works perfect and answers exactly my question.

R.W.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top