Solved

# Specify default file name for SAVE with COMDLG32.dll

Posted on 2006-11-14
406 Views
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
Question by:Kaprice
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 5
• 4

Author Comment

ID: 17943170
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 LVL 65 Expert Comment ID: 17974876 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 Comment

ID: 17976557
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

LVL 65

Expert Comment

ID: 17977494
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 Comment

ID: 17977517
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

LVL 65

Expert Comment

ID: 17990469
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 Comment

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

LVL 65

Accepted Solution

ID: 17991324
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 Comment

ID: 17991512
Right.  That's what I'll test.  I'll report back. Thanks!
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
###### Suggested Courses
Course of the Month5 days, 6 hours left to enroll