Dir Function Example This example uses the Dir function to check if certain files and directories exist. On the Macintosh, “HD:” is the default drive name and portions of the pathname are separated by colons instead of backslashes. Also, the Microsoft Windows wildcard characters are treated as valid file-name characters on the Mac. However, you can use the MacID function to specify file groups.
Dim MyFile, MyPath, MyName ' Returns "WIN.INI" (on Microsoft Windows) if it exists. MyFile = Dir("C:\WINDOWS\WIN.INI")
' Returns filename with specified extension. If more than one *.ini ' file exists, the first file found is returned. MyFile = Dir("C:\WINDOWS\*.INI")
' Call Dir again without arguments to return the next *.INI file in the ' same directory. MyFile = Dir
' Return first *.TXT file with a set hidden attribute. MyFile = Dir("*.TXT", vbHidden)
' Display the names in C:\ that represent directories. MyPath = "c:\" ' Set the path. MyName = Dir(MyPath, vbDirectory) ' Retrieve the first entry. Do While MyName <> "" ' Start the loop. ' Ignore the current directory and the encompassing directory. If MyName <> "." And MyName <> ".." Then ' Use bitwise comparison to make sure MyName is a directory. If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then Debug.Print MyName ' Display entry only if it End If ' it represents a directory. End If MyName = Dir ' Get next entry. Loop
Mange tak for dit svar! Men jeg vil gerne kunne UDPEJE et bibliotek fra brugerfladen, så en ikke kodekyndig kan vælge et bibliotek. Dvs: 1) åbne en sti, ligesom når man skal åbne en fil. 2) browse rundt og herefter klikke på et biblioteksnavn 3) få en msgbox op : " du har valg biblioteket c:\....\bibliotek"
Her var vi måske en en af VBA's begrænsninger. Det ville der være et oplagt til at kunne gøre, synes du ikke? Det eneste jeg, nemlig kan komme på, er at bruge:
Function GetImportFileName(explanation As String, FilterIndex) As String
' Set up list of file filters Filt = "Text Files (*.txt),*.txt," & _ "Lotus Files (*.prn),*.prn," & _ "Comma Separated Files (*.csv),*.csv," & _ "ASCII Files (*.asc),*.asc," & _ "Excel Files (*.xls),*.xls," & _ "Lotus Files (*.wk1),*.wk1," & _ "All Files (*.csv),*.csv" ' Display *.* by default
' Get the file name Filename = Application.GetOpenFilename _ (FileFilter:=Filt, _ FilterIndex:=FilterIndex, _ Title:=explanation)
' Exit if dialog box canceled If Filename = False Then MsgBox "No file was selected." Exit Function End If GetImportFileName = Filename
End Function
hvorfra men kan få returneret en fil. Men i stedet for at få smidt filen ud til brugeren så sender man path for filen ud. Men det kræver et der ER en fil i det bibliotek, som man skal vælge og at brugeren skal udpege en fil for at få vist et bibliotek. Det er også lidt omvendt -ikke.
Tak for indsatsen - hvis du får en ny ide så meld ENDELIG tilbage.
' ************************ STIFINDER PROGRAMMET`S START ******************** Private Type BROWSEINFO hOwner As Long pidlRoot As Long pszDisplayName As String lpszTitle As String ulFlags As Long lpfn As Long lParam As Long iImage As Long End Type
Private Declare Function SHGetPathFromIDList Lib "Shell32.dll" Alias _ "SHGetPathFromIDListA" (ByVal pidl As Long, _ ByVal pszPath As String) As Long
Private Declare Function SHBrowseForFolder Lib "Shell32.dll" Alias _ "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) _ As Long
Private Const BIF_RETURNONLYFSDIRS = &H1 Public Function BrowseFolder(szDialogTitle As String) As String Dim X As Long, bi As BROWSEINFO, dwIList As Long Dim szPath As String, wPos As Integer
With bi .hOwner = hWndAccessApp .lpszTitle = szDialogTitle .ulFlags = BIF_RETURNONLYFSDIRS End With
If X Then wPos = InStr(szPath, Chr(0)) BrowseFolder = Left$(szPath, wPos - 1) Else BrowseFolder = "" End If End Function ' ************************ STIFINDER PROGRAMMET`S SLUT ********************
Public Sub FindPath() mypath = BrowseFolder("Vælg stien til dine filerne") ' KALDER STIFINDER PROGRAMMET MsgBox " du valgte " & mypath End Sub
Public Sub minMakro() Dim MinSti As String MinSti = UseFileDialogOpen MsgBox MinSti End Sub
Function UseFileDialogOpen() As String ' Open the file dialog With Application.FileDialog(msoFileDialogFolderPicker) .AllowMultiSelect = False .Show UseFileDialogOpen = .SelectedItems(1) End With End Function
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.