Hello people,
I got this code to a Unicode aware Save As Dialog. It all works fine, but occasionally it would result the APP to crash if I SAVE on the 2nd or 3rd time etc.. I can't figure out what's wrong... Any ideas/suggestions?
Code:Private Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameW" (pOpenfilename As OPENFILENAME) As Long Private Type OPENFILENAME lStructSize As Long 'Length of structure, in bytes hWndOwner As Long 'Window that owns the dialog, or NULL hInstance As Long 'Handle of mem object containing template (not used) lpstrFilter As Long 'File types/descriptions, delimited with vbnullchar, ends with 2xvbnullchar lpstrCustomFilter As Long 'Filters typed in by user nMaxCustFilter As Long 'Length of CustomFilter, min 40x chars nFilterIndex As Long 'Filter Index to use (1,2,etc) or 0 for custom lpstrFile As Long 'Initial file/returned file(s), delimited with vbnullchar for multi files nMaxFile As Long 'Size of Initial File long , min 256 lpstrFileTitle As Long 'File.ext excluding path nMaxFileTitle As Long 'Length of FileTitle lpstrInitialDir As Long 'Initial file dir, null for current dir lpstrTitle As Long 'Title bar of dialog flags As Long 'See OFN_Flags nFileOffset As Integer 'Offset to file name in full path, 0-based nFileExtension As Integer 'Offset to file ext in full path, 0-based (excl '.') lpstrDefExt As Long 'Default ext appended, excl '.', max 3 chars lCustData As Long 'Appl defined data for lpfnHook lpfnHook As Long 'Pointer to hook procedure lpTemplateName As Long 'Template Name (not used) pvReserved As Long 'new Win2000 / WinXP members dwReserved As Long 'new Win2000 / WinXP members FlagsEx As Long 'new Win2000 / WinXP members End Type '//------------------ Public Function cDlgShowSave(ByVal InitialDir As String, _ ByVal DialogTitle As String, ByVal frmHwnd As Long, _ Optional Filter As String = "Text File(*.txt)|*.txt", _ Optional FileTitle As String = vbNullString, _ Optional ByRef rtnFilter As Long = 1) As String On Error GoTo ErrFound Dim OpenFile As OPENFILENAME Dim lReturn As Long Dim FileFilter As String Dim strFile As String strFile = String$(512, 0) If Not FileTitle = vbNullString Then strFile = FileTitle & strFile End If OpenFile.lStructSize = Len(OpenFile) OpenFile.hWndOwner = frmHwnd OpenFile.hInstance = App.hInstance FileFilter = Replace$(Filter, "|", vbNullChar) If AscW(Right$(FileFilter, 1)) <> 0& Then FileFilter = FileFilter & vbNullChar OpenFile.lpstrFilter = StrPtr(FileFilter) OpenFile.nFilterIndex = 1 OpenFile.lpstrFile = StrPtr(strFile) OpenFile.nMaxFile = Len(strFile) - 1 OpenFile.lpstrFileTitle = StrPtr(OpenFile.lpstrFile) OpenFile.nMaxFileTitle = StrPtr(OpenFile.nMaxFile) OpenFile.lpstrInitialDir = StrPtr(InitialDir) OpenFile.lpstrTitle = StrPtr(DialogTitle) OpenFile.flags = 0 lReturn = GetSaveFileName(OpenFile) If lReturn = 0 Then 'Cancel Button Pressed Else cDlgShowSave = Left$(strFile, InStr(strFile, vbNullChar) - 1) rtnFilter = OpenFile.nFilterIndex End If Exit Function ErrFound: MsgBox Err.Description, vbCritical, "File Save" cDlgShowSave = "" End Function






Reply With Quote
