Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Suggest/default filename using GetSaveFilename?

Status
Not open for further replies.

utc13

Programmer
Oct 25, 2001
43
US
I have been using the GetOpenFileName dialog with much success lately. I now have a need for the GeSaveFileName dialog which I have successfully converted to using my old code and some previous posts. However when a user activates the Save dialog, I would like to suggest a filename by placing it in the filename field. Can anyone tell me how to do this?
 
If you post your code for it would be alot easier for me to help you with this Regards,
gkprogrammer
 
Here's the module that I originally used for GetOpenFileName but have modified it for GetSaveFileName...


'Option Compare Database
Option Explicit

Public strLastPathRetrieved As String

Const OFN_READONLY = &H1
Const OFN_OVERWRITEPROMPT = &H2
Const OFN_HIDEREADONLY = &H4
Const OFN_NOCHANGEDIR = &H8
Const OFN_SHOWHELP = &H10
Const OFN_ENABLEHOOK = &H20
Const OFN_ENABLETEMPLATE = &H40
Const OFN_ENABLETEMPLATEHANDLE = &H80
Const OFN_NOVALIDATE = &H100
Const OFN_ALLOWMULTISELECT = &H200
Const OFN_EXTENSIONDIFFERENT = &H400
Const OFN_PATHMUSTEXIST = &H800
Const OFN_FILEMUSTEXIST = &H1000
Const OFN_CREATEPROMPT = &H2000
Const OFN_SHAREAWARE = &H4000
Const OFN_NOREADONLYRETURN = &H8000
Const OFN_NOTESTFILECREATE = &H10000
Const OFN_NONETWORKBUTTON = &H20000
Const OFN_NOLONGNAMES = &H40000
Const OFN_EXPLORER = &H80000
Const OFN_NODEREFERENCELINKS = &H100000
Const OFN_LONGNAMES = &H200000

Const OFN_SHAREFALLTHROUGH = 2
Const OFN_SHARENOWARN = 1
Const OFN_SHAREWARN = 0

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 Sub RetrievePathForAssociatedFile()

Dim OFName As OPENFILENAME
'Const OFN_HIDEREADONLY = &H1
OFName.lStructSize = Len(OFName)
'Select a filter
'OFName.lpstrFilter = "Text Files (*.txt)" + Chr$(0) + "*.txt" + Chr$(0) + "All Files (*.*)" + Chr$(0) + "*.*" + Chr$(0)
OFName.lpstrFilter = "All Files (*.*)" + Chr$(0) + "*.*"
'create a buffer for the file
OFName.lpstrFile = Space$(254)
'OFName.lpstrFile = "i.jpeg"
'set the maximum length of a returned file
OFName.nMaxFile = 255
'Create a buffer for the file title
OFName.lpstrFileTitle = Space$(254)
'Set the maximum length of a returned file title
OFName.nMaxFileTitle = 255
'Set the initial directory
OFName.lpstrInitialDir = "C:"
'Set the title
OFName.lpstrTitle = "Dialog Title Here..."
'No flags
'OFName.flags = 0
OFName.flags = OFN_HIDEREADONLY Or OFN_PATHMUSTEXIST Or OFN_FILEMUSTEXIST

'Show the 'Open File'-dialog
If GetSaveFileName(OFName) Then
strLastPathRetrieved = Trim$(OFName.lpstrFile)
Else
strLastPathRetrievedForAssociatedFile = ""
End If

End Sub



 
Hello utc13,

Change the value of the following to the suggested name:

OFName.lpstrFile

ie. OFName.lpstrFile = "C:\temp.txt" Regards,
gkprogrammer
 
Unfortunately I tried that already and I either get an 'Out of memory' error message or the procedure returns an incomplete/truncated path string. Any thoughts?
 
I just tried the code you supplied with the change that I suggested and it works fine for me, I am not exactly sure why you would be having a problem with this. Regards,
gkprogrammer
 
OK here's the code I just ran (please note the change you suggested)...


*************

'Option Compare Database
Option Explicit

Public strLastPathRetrieved As String

Const OFN_READONLY = &H1
Const OFN_OVERWRITEPROMPT = &H2
Const OFN_HIDEREADONLY = &H4
Const OFN_NOCHANGEDIR = &H8
Const OFN_SHOWHELP = &H10
Const OFN_ENABLEHOOK = &H20
Const OFN_ENABLETEMPLATE = &H40
Const OFN_ENABLETEMPLATEHANDLE = &H80
Const OFN_NOVALIDATE = &H100
Const OFN_ALLOWMULTISELECT = &H200
Const OFN_EXTENSIONDIFFERENT = &H400
Const OFN_PATHMUSTEXIST = &H800
Const OFN_FILEMUSTEXIST = &H1000
Const OFN_CREATEPROMPT = &H2000
Const OFN_SHAREAWARE = &H4000
Const OFN_NOREADONLYRETURN = &H8000
Const OFN_NOTESTFILECREATE = &H10000
Const OFN_NONETWORKBUTTON = &H20000
Const OFN_NOLONGNAMES = &H40000
Const OFN_EXPLORER = &H80000
Const OFN_NODEREFERENCELINKS = &H100000
Const OFN_LONGNAMES = &H200000

Const OFN_SHAREFALLTHROUGH = 2
Const OFN_SHARENOWARN = 1
Const OFN_SHAREWARN = 0

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 Sub RetrievePathForAssociatedFile()

Dim OFName As OPENFILENAME
'Const OFN_HIDEREADONLY = &H1
OFName.lStructSize = Len(OFName)
'Select a filter
'OFName.lpstrFilter = "Text Files (*.txt)" + Chr$(0) + "*.txt" + Chr$(0) + "All Files (*.*)" + Chr$(0) + "*.*" + Chr$(0)
OFName.lpstrFilter = "All Files (*.*)" + Chr$(0) + "*.*"
'create a buffer for the file


'OFName.lpstrFile = Space$(254)
OFName.lpstrFile = "temp.txt"


'set the maximum length of a returned file
OFName.nMaxFile = 255
'Create a buffer for the file title
OFName.lpstrFileTitle = Space$(254)
'Set the maximum length of a returned file title
OFName.nMaxFileTitle = 255
'Set the initial directory
OFName.lpstrInitialDir = "C:"
'Set the title
OFName.lpstrTitle = "Dialog Title Here..."
'No flags
'OFName.flags = 0
OFName.flags = OFN_HIDEREADONLY Or OFN_PATHMUSTEXIST Or OFN_FILEMUSTEXIST

'Show the 'Open File'-dialog
If GetSaveFileName(OFName) Then
strLastPathRetrieved = Trim$(OFName.lpstrFile)
MsgBox strLastPathRetrieved
Else
strLastPathRetrieved = ""
End If

End Sub

*************

When I run the module 'temp.txt' does indeed appear in the filename field. However when I select a directory location (in this case I chose the root C: drive) and click 'Save' the procedure returns only 'C:\temp.' instead of 'C:\temp.txt'.

It seems as though the variable holding the path is limited to only the number of characters that my suggested filename was.

Does this happen with you too?

I am using Access 2002 and Windows 98.
 
Hi utc13,

Try this:

strLastPathReceived = Left$(Trim$(OFName.lpstrFile), OFName.nFileOffset) & Left$(Trim$(OFName.lpstrFileTitle), Len(Trim$(OFName.lpstrFileTitle)) - 1)


You should now have the full pathname including the Extension. Let me know if this helps. Regards,
gkprogrammer
 
I made your suggested change and it indeed works when I chose the root C: location. The module returns 'C:\temp.txt' as it should.

However when I run it again and chose the desktop as the location the procedure returns 'C:\WINDOtemp.txt' instead of 'C:\WINDOWS\Desktop\temp.txt'.

It still seems that the variable holding the path either gets truncated or is limited in # of characters.

I kind of found a way around it. Instead of...

OFName.lpstrFile = "C:\temp.txt"

I used the same but added a bunch of spaces after it. Like...

OFName.lpstrFile = "C:\temp.txt "

As a result the variable buffer is now bigger (by the number of spaces added) and the full path now comes through.
 
I'm sorry, I forgot to add the additional line that will increase the MaxFileSize. Refer Below:

OFName.lpstrFile = "temp.txt" & String$(255 - Len(FileName) + 1, 0)
Regards,
gkprogrammer
 
In the email above please replace the FileName as "temp.txt" or whatever your default value will be. Regards,
gkprogrammer
 
Yea I caught the 'Filename' oversight. Everything now works as it should. Thanks so much for helping me work this out.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top