/ Forside/ Teknologi / Administrative / MS-Office / Spørgsmål
Login
Glemt dit kodeord?
Brugernavn

Kodeord


Reklame
Top 10 brugere
MS-Office
#NavnPoint
sion 18709
refi 14474
Klaudi 9389
Rosco40 5695
berpox 5456
dk 5398
webnoob 4919
Benjamin... 4870
o.v.n. 4637
10  EXTERMINA.. 4373
mappeliste i Excel.
Fra : DJfrakja
Vist : 529 gange
100 point
Dato : 26-02-08 10:48

Hvis jeg har en mappe med en masse filer (eks. *.doc), kan jeg så importere titel af dem ind i et Excel ark.?



 
 
Kommentar
Fra : Peder99


Dato : 26-02-08 14:08

Hej.

Du skal indsætte en commandbutton i dit excel ark. og der efter kopiere følgende kode ind:

Private Sub CommandButton1_Click()
Dim Message, Title, Default, Folder As String
Message = "Enter a folder destination" ' Set prompt.
Title = "Folder" ' Set title.
Default = "c:\" ' Set default.
' Display message, title, and default value.
Folder = InputBox(Message, Title, Default)
Dim Files As Variant
Files = qfil_GetAllFileNamesInDirectory(Folder)
i = 2 'Start row
For Each Filename In Files
Sheet1.Cells(i, "A") = Filename
i = i + 1
Next
End Sub

Function qfil_GetAllFileNamesInDirectory(strDirectoryName As String)

'declarations
Dim ra() As String
Dim objDirectory As Variant
Dim objFile As Variant
Dim intNumberOfFiles As Integer
Dim strFileName As String
Dim intIndex As Integer

'variables
Set objDirectory = qfil_GetDirectory(strDirectoryName)
intNumberOfFiles = objDirectory.Files.Count
ReDim Preserve ra(intNumberOfFiles)

'run through and get pathAndFileNames
intIndex = 0
For Each objFile In objDirectory.Files

'variables
strFileName = objFile.Name

'assign it
ra(intIndex) = strFileName

'increment
intIndex = intIndex + 1

Next

qfil_GetAllFileNamesInDirectory = ra

End Function


'method: get directory object
Function qfil_GetDirectory(strDirectoryName As String)

'declarations
Dim objFSO As Variant
Dim objDirectory As Variant

'variables
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objDirectory = objFSO.GetFolder(strDirectoryName)
Set qfil_GetDirectory = objDirectory

End Function

håber du kan få det til at virke!

Peder

Kommentar
Fra : Peder99


Dato : 04-03-08 09:03

Har du fået det til at virke?

Peder

Kommentar
Fra : IPAA


Dato : 01-05-08 21:57

Prøv f. eks. denne version:

Option Explicit

Function BrowseForFolder(Optional OpenAt As Variant) As Variant
'Function purpose: To Browser for a user selected folder.
'If the "OpenAt" path is provided, open the browser at that directory
'NOTE: If invalid, it will open at the Desktop level

Dim ShellApp As Object

'Create a file browser window at the default folder
Set ShellApp = CreateObject("Shell.Application"). _
BrowseForFolder(0, "Please choose a folder", 0, OpenAt)

'Set the folder to that selected. (On error in case cancelled)
On Error Resume Next
BrowseForFolder = ShellApp.self.Path
On Error GoTo 0

'Destroy the Shell Application
Set ShellApp = Nothing

'Check for invalid or non-entries and send to the Invalid error
'handler if found
'Valid selections can begin L: (where L is a letter) or
'\\ (as in \\servername\sharename. All others are invalid
Select Case Mid(BrowseForFolder, 2, 1)
Case Is = ":"
If Left(BrowseForFolder, 1) = ":" Then GoTo Invalid
Case Is = "\"
If Not Left(BrowseForFolder, 1) = "\" Then GoTo Invalid
Case Else
GoTo Invalid
End Select

Exit Function

Invalid:
'If it was determined that the selection was invalid, set to False
BrowseForFolder = False

End Function
Function qfil_GetAllFileNamesInDirectory(strDirectoryName As String)

'declarations
Dim ra() As String
Dim objDirectory As Variant
Dim objFile As Variant
Dim intNumberOfFiles As Integer
Dim strFileName As String
Dim intIndex As Integer

'variables
Set objDirectory = qfil_GetDirectory(strDirectoryName)
intNumberOfFiles = objDirectory.Files.Count
ReDim Preserve ra(intNumberOfFiles)

'run through and get pathAndFileNames
intIndex = 0
For Each objFile In objDirectory.Files

'variables
strFileName = objFile.Name

'assign it
ra(intIndex) = strFileName

'increment
intIndex = intIndex + 1

Next

qfil_GetAllFileNamesInDirectory = ra

End Function


'method: get directory object
Function qfil_GetDirectory(strDirectoryName As String)

'declarations
Dim objFSO As Variant
Dim objDirectory As Variant

'variables
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objDirectory = objFSO.GetFolder(strDirectoryName)
Set qfil_GetDirectory = objDirectory
End Function

Sub Knap4_Klik()
Dim fs, f, fn, Files As Variant, i As Integer, fp As String

Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(BrowseForFolder())
MsgBox (f.Path)
fp = f.Path
Files = qfil_GetAllFileNamesInDirectory(fp)
MsgBox (Files(1))
i = 2 'Start row
For Each fn In Files
Worksheets("Ark1").Cells(i, "A") = fn
i = i + 1
Next

End Sub

Du har følgende muligheder
Dette spørgsmål er blevet annulleret, det er derfor ikke muligt for at tilføje flere kommentarer.
Søg
Reklame
Statistik
Spørgsmål : 177423
Tips : 31962
Nyheder : 719565
Indlæg : 6407900
Brugere : 218876

Månedens bedste
Årets bedste
Sidste års bedste