Solved

Specify default file name for SAVE with COMDLG32.dll

Posted on 2006-11-14
12
391 Views
Last Modified: 2008-02-01
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
Comment
Question by:Kaprice
  • 5
  • 4
12 Comments
 

Author Comment

by:Kaprice
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

by:rockiroads
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

by:Kaprice
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

by:rockiroads
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:Kaprice
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

by:rockiroads
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

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

Accepted Solution

by:
rockiroads earned 250 total points
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

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

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now