Private Declare PtrSafe Function GetOpenFileName Lib "comdlg32.dll" Alias _"GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As LongPrivate Declare PtrSafe Function GetSaveFileName Lib "comdlg32.dll" Alias _"GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As LongPrivate Declare PtrSafe Function SHBrowseForFolder Lib "shell32.dll" _Alias "SHBrowseForFolderA" (lpBrowseInfo As BrowseInfo) As LongPrivate Declare PtrSafe Function SHGetPathFromIDList Lib "shell32.dll" _(ByVal pidList As LongPtr, ByVal lpBuffer As String) As LongPrivate Declare PtrSafe Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As LongPtr)Private Const BIF_RETURNONLYFSDIRS As Long = 1Private Const CSIDL_DRIVES As Long = &H11Private Const WM_USER As Long = &H400Private Const MAX_PATH As Long = 260'// message from browserPrivate Const BFFM_INITIALIZED As Long = 1Private Const BFFM_SELCHANGED As Long = 2Private Const BFFM_VALIDATEFAILEDA As Long = 3 '// lParam:szPath ret:1(cont),0(EndDialog)Private Const BFFM_VALIDATEFAILEDW As Long = 4 '// lParam:wzPath ret:1(cont),0(EndDialog)Private Const BFFM_IUNKNOWN As Long = 5 '// provides IUnknown to client. lParam: IUnknown*'// messages to browserPrivate Const BFFM_SETSTATUSTEXTA As Long = WM_USER + 100Private Const BFFM_ENABLEOK As Long = WM_USER + 101Private Const BFFM_SETSELECTIONA As Long = WM_USER + 102Private Const BFFM_SETSELECTIONW As Long = WM_USER + 103Private Const BFFM_SETSTATUSTEXTW As Long = WM_USER + 104Private Const BFFM_SETOKTEXT As Long = WM_USER + 105 '// Unicode onlyPrivate Const BFFM_SETEXPANDED As Long = WM_USER + 106 '// Unicode onlyPrivate Type OPENFILENAMElStructSize As LonghWndOwner As LongPtrhInstance As LongPtrlpstrFilter As StringlpstrCustomFilter As StringnMaxCustFilter As LongnFilterIndex As LonglpstrFile As StringnMaxFile As LonglpstrFileTitle As StringnMaxFileTitle As LonglpstrInitialDir As StringlpstrTitle As Stringflags As LongnFileOffset As IntegernFileExtension As IntegerlpstrDefExt As StringlCustData As LonglpfnHook As LongPtrlpTemplateName As StringEnd Type'====== File Browsers for 64 bit VBA 7 ========Public Function FileBrowseOpen(ByVal sInitFolder As String, _ByVal sTitle As String, _ByVal sFilter As String, _ByVal nFilterIndex As Integer) As StringDim OpenFile As OPENFILENAMEDim lReturn As LongsInitFolder = CorrectPath(sInitFolder)OpenFile.lpstrInitialDir = sInitFolder' Swap filter separator for api separatorsFilter = Replace(sFilter, "|", Chr(0))OpenFile.lpstrFilter = sFilterOpenFile.nFilterIndex = nFilterIndexOpenFile.lpstrTitle = sTitleOpenFile.hWndOwner = 0OpenFile.lpstrFile = String(257, 0)OpenFile.nMaxFile = LenB(OpenFile.lpstrFile) - 1OpenFile.lStructSize = LenB(OpenFile)OpenFile.lpstrFileTitle = OpenFile.lpstrFileOpenFile.nMaxFileTitle = OpenFile.nMaxFileOpenFile.flags = 0lReturn = GetOpenFileName(OpenFile)If lReturn = 0 ThenFileBrowseOpen = ""ElseFileBrowseOpen = Trim(Left(OpenFile.lpstrFile, InStr(1, OpenFile.lpstrFile, vbNullChar) - 1))End IfEnd FunctionPublic Function FileBrowseSave(ByVal sDefaultFilename As String, _ByVal sInitFolder As String, _ByVal sTitle As String, _ByVal sFilter As String, _ByVal nFilterIndex As Integer) As StringDim PadCount As IntegerDim OpenFile As OPENFILENAMEDim lReturn As LongsInitFolder = CorrectPath(sInitFolder)' Swap filter separator for api separatorsFilter = Replace(sFilter, "|", Chr(0))OpenFile.lpstrFilter = sFilterOpenFile.nFilterIndex = 1OpenFile.hWndOwner = 0PadCount = 260 - Len(sDefaultFilename)OpenFile.lpstrFile = sDefaultFilename & String(PadCount, Chr(0))'OpenFile.lpstrFile = String(257, 0)OpenFile.nMaxFile = LenB(OpenFile.lpstrFile) - 1OpenFile.lStructSize = LenB(OpenFile)OpenFile.lpstrFileTitle = OpenFile.lpstrFileOpenFile.nMaxFileTitle = OpenFile.nMaxFileOpenFile.lpstrInitialDir = sInitFolderOpenFile.lpstrTitle = sTitleOpenFile.flags = 0lReturn = GetSaveFileName(OpenFile)If lReturn = 0 ThenFileBrowseSave = ""ElseFileBrowseSave = Trim(Left(OpenFile.lpstrFile, InStr(1, OpenFile.lpstrFile, vbNullChar) - 1))End IfEnd Function'====== Folder Browser for 64 bit VBA 7 ========Public Function FolderBrowse(ByVal sDialogTitle As String, ByVal sInitFolder As String) As StringDim ReturnPath As StringDim b(MAX_PATH) As ByteDim pItem As LongDim sFullPath As StringDim bi As BrowseInfoDim ppidl As LongsInitFolder = CorrectPath(sInitFolder)' Note VBA windows and dialogs do not have an hWnd property.bi.hWndOwner = 0 'Windows Main Screen handle.' SHGetSpecialFolderLocation bi.hWndOwner, CSIDL_DRIVES, ppidlbi.pIDLRoot = 0 'ppidlbi.pszDisplayName = VarPtr(b(0))bi.lpszTitle = sDialogTitlebi.ulFlags = BIF_RETURNONLYFSDIRSIf FolderExists(sInitFolder) Then bi.lpfnCallback = PtrToFunction(AddressOf BFFCallback)bi.lParam = StrPtr(sInitFolder)pItem = SHBrowseForFolder(bi)If pItem Then ' SucceededsFullPath = Space$(MAX_PATH)If SHGetPathFromIDList(pItem, sFullPath) ThenReturnPath = Left$(sFullPath, InStr(sFullPath, vbNullChar) - 1) ' Strip nullsCoTaskMemFree pItemEnd IfEnd IfIf ReturnPath <> "" ThenIf Right$(ReturnPath, 1) <> "\" ThenReturnPath = ReturnPath & "\"End IfEnd IfFolderBrowse = ReturnPathEnd FunctionPrivate Function PtrToFunction(ByVal lFcnPtr As LongPtr) As LongPtrPtrToFunction = lFcnPtrEnd FunctionPrivate Function CorrectPath(ByVal sPath As String) As StringIf Right$(sPath, 1) = "\" ThenIf Len(sPath) > 3 Then sPath = Left$(sPath, Len(sPath) - 1) ' Strip backslash from non-rootElseIf Len(sPath) = 2 Then sPath = sPath & "\" ' Append backslash to rootEnd IfCorrectPath = sPathEnd FunctionPublic Function FolderExists(ByVal sFolderName As String) As BooleanDim att As LongOn Error Resume Nextatt = GetAttr(sFolderName)If Err.Number = 0 ThenFolderExists = TrueElseErr.ClearFolderExists = FalseEnd IfOn Error GoTo 0End Function
Public Type BrowseInfohWndOwner As LongPtrpIDLRoot As LongPtrpszDisplayName As StringlpszTitle As StringulFlags As LonglpfnCallback As LongPtrlParam As LongPtriImage As LongEnd TypePublic Declare PtrSafe Function SendMessageA Lib "user32" _(ByVal Hwnd As LongPtr, ByVal wMsg As Long, _ByVal wParam As LongPtr, lParam As Any) As LongPtr' typedef int (CALLBACK* BFFCALLBACK)(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData);Public Function BFFCallback(ByVal Hwnd As LongPtr, ByVal uMsg As LongPtr, ByVal lParam As LongPtr, ByVal sData As String) As LongPtrIf uMsg = BFFM_INITIALIZED ThenSendMessageA Hwnd, BFFM_SETSELECTIONA, True, ByVal sDataEnd IfEnd Function
Public Sub test123()Dim strTEst As StringstrTEst = FileBrowseOpen("C:\", "Test", "*.*", 1)MsgBox (strTEst)End Sub
This document governs the use of our Community Forum. By registering and using the platform, you accept these conditions.
The COPA-DATA Community Forum serves to encourage the exchange of information and experience about the zenon software between forum users respectively zenon users.
Please mind that any published information on the Community Forum is the subjective opinion and view based on the experience and the level of knowledge of the author. COPA-DATA does not overtake any responsibility for the content and the accuracy of the shared information.
Users of the Community Forum are encouraged to share only well-founded experiences and to point out any risks associated with the implementation of proposed solutions to problems. COPA-DATA at its absolute discretion, reserves the right to moderate the forum. In this connection COPA-DATA may remove any information containing false facts, potentially dangerous solutions, bad language or content that may insult, degrade or discriminate others. COPA-DATA may block a non-complying user from forum access if the user violated this provision.
COPA-DATA reserves the right to change this document from time to time at own discretion.
Ing. Punzenberger COPA-DATA GmbH
Karolingerstraße 7b · 5020 Salzburg · Austria
www.copadata.com