Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 422
• Last Modified:

# Specify default file name for SAVE with COMDLG32.dll

I have code I've taken and modified (a bit) from one of Ken Getz's books for using COMDLG32.dll to show the Save As dialog box and return the path and file name.

It works great UNLESS I specify a default file name -- it that case it throws an error.

Can anyone suggest code for doing that?

I'll post my code in a reply.

-Kaprice
0
Kaprice
Asked:
• 5
• 4
1 Solution

Author Commented:
Here's the code that throws the error.

If I provide a default filename, it shows properly in the dialog box.  But, when I click the Save button, I get an error, and typOpenFile.lpstrFile holds only the path, not the file name.

Dim strReturn     As String
Dim typOpenFile   As API_OPENFILENAME

typOpenFile.lStructSize = Len(typOpenFile)
typOpenFile.hWndOwner = Application.hWndAccessApp
typOpenFile.hInstance = 0
typOpenFile.lpstrFilter = zms_CreateFilterString(IIf(Len(pstrFilter) = 0, zms_CreateFilterString(mcstrDefaultFilter), pstrFilter))
'typOpenFile.lpstrCustomFilter
'typOpenFile.nMaxCustrFilter
typOpenFile.nFilterIndex = 0
typOpenFile.lpstrFile = String(512, 0)
typOpenFile.nMaxFile = 511
typOpenFile.lpstrFileTitle = String$(512, 0) typOpenFile.nMaxFileTitle = 511 typOpenFile.lpstrInitialDir = IIf(Len(pstrInitialDir) = 0, CurDir, pstrInitialDir) typOpenFile.lpstrTitle = pstrTitle typOpenFile.Flags = OFN_HIDEREADONLY Or OFN_SHOWHELP Or OFN_OVERWRITEPROMPT 'typOpenFile.nFileOffset 'typOpenFile.nFileExtension typOpenFile.lpstrDefExt = "" 'typOpenFile.lCustrData 'typOpenFile.lpfnHook 'typOpenFile.lpTemplateName ';typOpenFile.lpstrFile = pstrDefName 'typOpenFile.??? = pstrDefName ??? not sure where this goes ??? ' Call the Common dialog fOK = COMDLG32_GetSaveFileName(typOpenFile) strReturn = Left(typOpenFile.lpstrFile, InStr(typOpenFile.lpstrFile, vbNullChar) - 1) ---- Here's the Declaration: Type API_OPENFILENAME lStructSize As Long hWndOwner As Long hInstance As Long lpstrFilter As String lpstrCustomFilter As String nMaxCustrFilter 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 lCustrData As Long lpfnHook As Long lpTemplateName As String End Type Const OFN_ALLOWMULTISELECT = &H200 Const OFN_CREATEPROMPT = &H2000 Const OFN_EXPLORER = &H80000 Const OFN_FILEMUSTEXIST = &H1000 Const OFN_HIDEREADONLY = &H4 Const OFN_NOCHANGEDIR = &H8 Const OFN_NODEREFERENCELINKS = &H100000 Const OFN_NONETWORKBUTTON = &H20000 Const OFN_NOREADONLYRETURN = &H8000 Const OFN_NOVALIDATE = &H100 Const OFN_OVERWRITEPROMPT = &H2 Const OFN_PATHMUSTEXIST = &H800 Const OFN_READONLY = &H1 Const OFN_SHOWHELP = &H10 Declare Function COMDLG32_GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" _ (pOpenfilename As API_OPENFILENAME) _ As Boolean Declare Function COMDLG32_GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" _ (pOpenfilename As API_OPENFILENAME) _ As Boolean -Kaprice 0 Commented: Now in the case of your code strReturn = Left(typOpenFile.lpstrFile, InStr(typOpenFile.lpstrFile, vbNullChar) - 1) if there is no vbnullchar, then u will end up doing a left on a negative. I guess because u set lpstrFile to a value, then there is no vbnullchar there. u can verify this by doing this before setting strReturn msgbox InStr(typOpenFile.lpstrFile, vbNullChar) so u should code like this - check for character, only do a left it it exists dim iPos as integer iPos = InStr(typOpenFile.lpstrFile, vbNullChar) if iPos = 0 then strReturn = typOpenFile.lpstrFile else strReturn = Left$(typOpenFile.lpstrFile, InStr(typOpenFile.lpstrFile, vbNullChar)-1)

0

Author Commented:
Well, that's a good catch, but in this case the reason there's no vbNullChar is because there no value at all in typOpenFile.lpstrFile.

The default file name shows in the dialog box.  And, I can type a new one in.  But, in no case does that value get stored in typOpenFile.lpstrFile.

Again, the value gets assigned perfectly if I don't assign a default value.

Any ideas as to why that would be?
0

Commented:
Hmm, dont know
I tried your exact code which is how I managed to find the no existence of vbnullchar

I specified a default value in lpstrFile and it got returned
I entered my own name in the dialog ,and that got returned also

I dont seem to be having the same probs as u

here is the test sample code Im using

Public Sub TestCd()

Dim strReturn     As String
Dim typOpenFile   As API_OPENFILENAME
Dim ipos As Integer

typOpenFile.lStructSize = Len(typOpenFile)
typOpenFile.hWndOwner = Application.hWndAccessApp
typOpenFile.hInstance = 0
typOpenFile.lpstrFilter = "*.txt"
'typOpenFile.lpstrCustomFilter
'typOpenFile.nMaxCustrFilter
typOpenFile.nFilterIndex = 0
typOpenFile.lpstrFile = String(512, 0)
typOpenFile.nMaxFile = 511
typOpenFile.lpstrFileTitle = String$(512, 0) typOpenFile.nMaxFileTitle = 511 typOpenFile.lpstrInitialDir = "C:\ee" typOpenFile.lpstrTitle = "test" typOpenFile.Flags = OFN_HIDEREADONLY Or OFN_SHOWHELP Or OFN_OVERWRITEPROMPT 'typOpenFile.nFileOffset 'typOpenFile.nFileExtension typOpenFile.lpstrDefExt = "" 'typOpenFile.lCustrData 'typOpenFile.lpfnHook 'typOpenFile.lpTemplateName ';typOpenFile.lpstrFile = pstrDefName 'typOpenFile.??? = pstrDefName ??? not sure where this goes ??? typOpenFile.lpstrFile = "c:\ee\xx.txt" ' Call the Common dialog COMDLG32_GetSaveFileName typOpenFile ipos = InStr(typOpenFile.lpstrFile, vbNullChar) If ipos = 0 Then strReturn = typOpenFile.lpstrFile Else strReturn = Left$(typOpenFile.lpstrFile, InStr(typOpenFile.lpstrFile, vbNullChar) - 1)

MsgBox strReturn
End Sub

0

Author Commented:
Hmmm.  The only think I can see that's different is that when you're storing the default value, you're using a full path.  Whereas I'm just giving it a file name.

Could that be the difference?  I'll test it, tomorrow.
0

Commented:
could it be that because lpstrFile returns the full pathname, it needs that when initialising?
that is the only conclusion I can come up with

so u can specify the start directory
but lpstrFile overrides this ?
0

Author Commented:
I haven't tested, yet.  But, the start directory is set independently with "lpstrInitialDir".
0

Commented:
yes, but what Im saying is perhaps the setting of the file overrides it, therefore u have to specify the full path

afterall, it does return the full path
0

Author Commented:
Right.  That's what I'll test.  I'll report back. Thanks!
0

## Featured Post

• 5
• 4
Tackle projects and never again get stuck behind a technical roadblock.