Attribute VB_Name = "modCommonDialog" Option Explicit '================================================================================ ' Common Dialog Box Functions ' ' This module contains the common dialog box functions and filters '================================================================================ '================================================================================ '==== Example Usage ==== '================================================================================ ' If (dlgOpen(cmnDlg, dlgMCA_Filter)) Then ' FileToOpen = cmnDlg.Filename ' Else ' 'a file was not selected ' End If ' ' If (dlgSave(cmnDlg, dlgCSV_Filter)) Then ' FileToSave = cmnDlg.Filename ' Else ' 'a file was not selected ' End If '================================================================================ 'dialog box file filters for file extensions Public Const dlgCFG_Filter As String = "Amptek Config File (*.cfg)|*.cfg|All Files (*.*)|*.*" Public Const dlgCSV_Filter As String = "Comma Separated Value (*.csv)|*.csv|All Files (*.*)|*.*" Public Const dlgMCA_Filter As String = "Amptek Spectrum (*.mca)|*.mca|All Files (*.*)|*.*" Public Const dlgXL_Filter As String = "Excel Files (*.xls)|*.xls|All Files (*.*)|*.*" 'creates an open common dialog, returns true is a file was selected, false otherwise Public Function dlgOpen(CmnDlgFIO As CommonDialog, strFileFilters As String) As Boolean On Error GoTo ErrHandlerOpen dlgOpen = False CmnDlgFIO.CancelError = True 'create error on cancel CmnDlgFIO.FilterIndex = 1 CmnDlgFIO.Filter = strFileFilters CmnDlgFIO.Flags = cdlOFNHideReadOnly 'cdlOFNFileMustExist + cdlOFNHideReadOnly CmnDlgFIO.filename = GetDefaultFileWildcard(strFileFilters) 'MsgBox GetDefaultFileWildcard(strFileFilters) CmnDlgFIO.ShowOpen dlgOpen = True Exit Function ErrHandlerOpen: If (Err.Number = 32755) Then 'If (error = 32755) user pressed Cancel button. '------ message from dlgbox to exit ------ Else MsgBox Err.Description & " (" & Err.Number & ")" End If Exit Function End Function 'creates a save common dialog, returns true if the file was saved, false otherwise Public Function dlgSave(CmnDlgFIO As Object, strFileFilters As String) As Boolean On Error GoTo ErrHandlerSave dlgSave = False CmnDlgFIO.CancelError = True 'create error on cancel CmnDlgFIO.FilterIndex = 1 CmnDlgFIO.Filter = strFileFilters CmnDlgFIO.Flags = cdlOFNHideReadOnly + cdlOFNOverwritePrompt CmnDlgFIO.filename = GetDefaultFileWildcard(strFileFilters) CmnDlgFIO.ShowSave dlgSave = True Exit Function ErrHandlerSave: If (Err.Number = 32755) Then 'If (error = 32755) user pressed Cancel button. 'message from dlgbox to exit Else MsgBox Err.Description & " (" & Err.Number & ")" End If Exit Function End Function Private Function GetDefaultFileWildcard(strFilter As String) As String Dim extBegin As Integer Dim extEnd As Integer Dim strExt As String GetDefaultFileWildcard = "" 'find starting bar, read until next bar or end of string extBegin = InStr(strFilter, "|") If (extBegin < 1) Then Exit Function extEnd = InStr(extBegin + 1, strFilter, "|") If ((extEnd - 1) < extBegin) Then extEnd = Len(strFilter) Else extEnd = extEnd - 1 End If strExt = Trim(Mid(strFilter, extBegin + 1, extEnd - extBegin)) If (Len(strExt) < 1) Then Exit Function GetDefaultFileWildcard = strExt End Function