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
KapriceAsked:
Who is Participating?
 
rockiroadsConnect With a Mentor 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
 
KapriceAuthor 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
 
rockiroadsCommented:
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
Easily Design & Build Your Next Website

Squarespaceā€™s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

 
KapriceAuthor 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
 
rockiroadsCommented:
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
 
KapriceAuthor 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
 
rockiroadsCommented:
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
 
KapriceAuthor Commented:
I haven't tested, yet.  But, the start directory is set independently with "lpstrInitialDir".
0
 
KapriceAuthor Commented:
Right.  That's what I'll test.  I'll report back. Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.