Common Dialog option through API
Posted on July 25, 2011
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | 'System & API - Common Dialog option through API Option Compare Database Option Explicit Public Const MaxDWord As Double = 4294967295# Public Const mMax_Path = 4096 Public Type typOpenFilename lStructSize As Long hwndOwner As Long hInstance As Long lpstrFilter As String lpstrCustomFilter As String nMaxCustFilter 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 lCustData As Long lpfnHook As Long lpTemplateName As String End Type Public Type typChooseColor lStructSize As Long hwndOwner As Long hInstance As Long rgbResult As Long lpCustColors As String Flags As Long lCustData As Long lpfnHook As Long lpTemplateName As String End Type Public Declare Function apiGetTickCount Lib "kernel32" Alias "GetTickCount" () As Long Public Declare Function apiGetOpenFilename Lib "comdlg32.dll" Alias "GetOpenFileNameA" (ptypOpenFilename As typOpenFilename) As Long Public Declare Function apiGetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" _ (ptypOpenFilename As typOpenFilename) As Long Public Declare Function apiGetChooseColor Lib "comdlg32.dll" Alias "ChooseColorA" _ (ptypChooseColor As typChooseColor) As Long '---- my design based on MS Help - Selects a colour using the common dialog box Public Function ColourSelection(Optional lngFlags, Optional lngInitColour) As Long '---- Uses the common dialog box / colour selection '---- usual flags are : 1 - starting colour specified, 2 - full open '---- so use 3 as the flag every time '---- Returns : '---- -1 : Failed/cancelled '---- >=0 : colour in long Dim cChoose As typChooseColor Dim lngRes As Long Dim intI As Integer Dim bytCols As Byte On Error Resume Next ColourSelection = -1 With cChoose '---- default info .lStructSize = Len(cChoose) .hwndOwner = 0 .hInstance = 0 .lpCustColors = StrConv(bytCols, vbUnicode) '---- optional bits .Flags = 0 .rgbResult = 0 If Not IsMissing(lngFlags) Then .Flags = lngFlags If Not IsMissing(lngInitColour) Then .rgbResult = lngInitColour lngRes = apiGetChooseColor(cChoose) If lngRes > 0 Then ColourSelection = .rgbResult End With If Not Err.Number = 0 Then ColourSelection = -1 End Function '---- Selects a/several file(s) using common dialogs Public Function FileSelection(ByVal strInitDir As String, ByVal lngFlag As Long, ByVal strFilter As String, _ ByVal lngFilterIndex As Long, ByVal blnOpen As Boolean, Optional strTitle, Optional strFileExtension, _ Optional lngOwnerHwnd) As String '---- Selects one or more filenames '---- Requires '---- inital directory to open in '---- flags for opening '---- usual flags are : '---- &H200000 - use long filenames '---- &H80000 - Explorer type window '---- &H200 - multi select '---- &H4 - hide read only option on box - v useful '---- &H8 - force same dir as when opened '---- &H1000 - file must exist - useful for opening '---- usual I use : &H280004 '---- Returns a string holding either : '---- nothing '---- path and filename '---- path chr$(0) filenames separated by chr$(0) '---- NOTE : current max chars = 257 ... I think expand if neccessary Dim strTemp As String Dim lngReturn As Long, lngP As Long, lngO As Long Dim cOpenFilename As typOpenFilename On Error Resume Next With cOpenFilename '---- Default values according to another developer .lStructSize = Len(cOpenFilename) .hInstance = 0 .nFilterIndex = 1 .nFileOffset = 0 .lpstrFile = String(5000, 0) .nMaxFile = Len(.lpstrFile) - 1 .lpstrFileTitle = .lpstrFile .nMaxFileTitle = .nMaxFile If Not IsMissing(strFileExtension) Then .lpstrDefExt = strFileExtension '---- Bits like the common dialog control If blnOpen Then .lpstrTitle = "Open a file..." Else .lpstrTitle = "Save file as..." End If If Not IsMissing(strTitle) Then .lpstrTitle = strTitle '---- messing '---- default filter '---- each filter is separated by a character of 0 - Name - filter - name filter (etc..) '---- example : ' .lpstrFilter = "All Files" & Chr$(0) & "*.*" & Chr$(0) & "Text Files" & Chr$(0) & "*.txt;*.csv" '---- replace filter with the selection chosen by the programmer... .lpstrFilter = "All Files (*.*)" & Chr$(0) & "*.*" lngO = 1 lngP = InStr(1, strFilter, "|") If lngP > 0 Then strTemp = "" Do Until lngP = 0 strTemp = strTemp & IIf(Len(strTemp) > 0, Chr$(0), "") & Mid$(strFilter, lngO, lngP - lngO) lngO = lngP + 1 lngP = InStr(lngP + 1, strFilter, "|") Loop strTemp = strTemp & Chr$(0) & Right$(strFilter, Len(strFilter) - lngO + 1) Else strTemp = strFilter End If .lpstrFilter = strTemp .Flags = lngFlag .hwndOwner = 0 If Not IsMissing(lngOwnerHwnd) Then .hwndOwner = lngOwnerHwnd '---- remark out the next line - or amend it as applicable If .hwndOwner = 0 Then .hwndOwner = Application.hWndAccessApp .lpstrInitialDir = strInitDir End With '---- is the dialog box an open or save? If blnOpen Then lngReturn = apiGetOpenFilename(cOpenFilename) Else lngReturn = apiGetSaveFileName(cOpenFilename) End If '---- send back the selected file(s) If lngReturn = 0 Then FileSelection = "" Else FileSelection = RemoveNonPChars(cOpenFilename.lpstrFile) End If End Function Public Function RemoveNonPChars(ByVal strText As String) As String '---- gets rid of the extra chr$(0)'s in the text '---- by looking for two chr$(0)'s together (only happens at the end...) '---- NOTE : only removes those at the end of the string - for multi means that you can get the filenames... Dim lngP As Long On Error Resume Next RemoveNonPChars = " " If Len(strText) = 0 Then Exit Function lngP = InStr(1, strText, Chr$(0) & Chr$(0)) If lngP > 1 Then RemoveNonPChars = Left$(strText, lngP - 1) If Not Err.Number = 0 Then RemoveNonPChars = " " Err.Clear End If End Function |