我找到了这段代码,但列出了按名称排序的文件名,我不知道如何改编它:
Dim MyPathAs String
Dim MyNameAs String
With Dialogs(wdDialogCopyFile)
If .Display() <> -1 Then Exit Sub
MyPath = .Directory
End With
If Len(MyPath) = 0 Then Exit Sub
If Asc(MyPath) = 34 Then
MyPath = Mid$(MyPath, 2, Len(MyPath) - 2)
End If
MyName = Dir$(MyPath& "*.*")
Do While MyName<> ""
Selection.InsertAfterMyName&vbCr
MyName = Dir
Loop
Selection.CollapsewdCollapseEnd
End Sub
答案 0 :(得分:3)
这是一种不同的方式。在Word VBA编辑器中:
工具&gt;参考文献...&gt;选中以下两项:
然后:
Dim iFil As Long
Dim FSO As FileSystemObject
Dim fil As File
Dim fld As Folder
Dim xlApp As Excel.Application
Dim sh As Excel.Worksheet
Dim rngTableTopLeft As Excel.Range
Set xlApp = New Excel.Application
Set sh = xlApp.Workbooks.Add.Sheets(1)
Set rngTableTopLeft = sh.Range("A1") ' or wherever; doesn't matter
'Put file names and date last modified in Excel sheet
Set FSO = New FileSystemObject
Set fld = FSO.GetFolder("C:\Users\jeacor\Documents")
For Each fil In fld.Files
iFil = iFil + 1
With rngTableTopLeft.Cells(iFil, 1)
.Value = fil.Name
.Offset(0, 1).Value = fil.DateLastModified
End With
Next fil
'Sort them by date last modified using Excel Sort function
With sh.Sort
.SortFields.Add Key:=rngTableTopLeft.Offset(0, 1).Resize(fld.Files.Count, 1), Order:=xlAscending
.SetRange rngTableTopLeft.Resize(fld.Files.Count, 2)
.Apply
End With
'Copy result to Word document
With rngTableTopLeft.Resize(fld.Files.Count, 2)
.EntireColumn.AutoFit
.Copy
End With
Selection.Paste
'Goodbye
xlApp.DisplayAlerts = False 'suppress the "exit without saving?" prompt
xlApp.Quit