我从这个论坛找到了代码。我已经非常努力地取得了成果,但在这方面失败了。设置 f = fs.GetFile(fwb)。运行时错误“53”我正在使用 Win 7、Office 2013。
我有一个 Excel 电子表格,其 A 列中有一个文件名。A 列中列出的文件名出现在一个或多个源目录中的一个或多个 Ms office .doc 文件中。
我需要 Excel 以递归方式搜索 .doc 文件,并将包含 A 列中指定的文件名的文件的路径返回到 B 列。如果多个文件转到 C 列等。
我急需这个宏。请有人帮助我。
__________________________________ __|__A_____|______B_____|_____ 1 | test_1.doc |c:\cost\test_1.doc| 2 | test_2.doc |c:\cost\test_2.doc|
Private Sub CommandButton1_Click()
Dim sh As Worksheet, rng As Range, lr As Long, fPath As String
Set sh = Sheets(1) 'Change to actual
lstRw = sh.Cells.Find(
What:="*",
After:=sh.Range("A1"),
LookAt:=xlPart,
LookIn:=xlFormulas,
SearchOrder:=xlByRows,
SearchDirection:=xlPrevious,
MatchCase:=False
).Row
Set rng = sh.Range("A2:A" & lstRw)
With Application.FileDialog(msoFileDialogFolderPicker)
.Show
fPath = .SelectedItems(1)
End With
If Right(fPath, 1) <> "\" Then
fPath = fPath & "\"
End If
fwb = Dir(fPath & "*.*")
x = 2
Do While fwb <> ""
For Each c In rng
If InStr(LCase(fwb), LCase(c.Value)) > 0 Then
Worksheets("Sheet2").Range("C" & x) = fwb
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFile(fwb) 'Run time error '53'
Worksheets("Sheet2").Range("D" & x) = f.DateLastModified
Worksheets("Sheet2").Range("B" & x) = f.Path
Worksheets("sheet2").Range("A" & x) = c.Value
Columns("A:D").AutoFit
Set fs = Nothing
Set f = Nothing
x = x + 1
End If
Next
fwb = Dir
Loop
Set sh = Nothing
Set rng = Nothing
Sheets(2).Activate
End Sub