[英]Search files for information contained in excel cell and return path
I have found codes from this forum. 我从该论坛找到了代码。 I have tried very hard to achieve the results but failed on this part.
我已经非常努力地实现了结果,但是在这一部分上却失败了。 Set f = fs.GetFile(fwb).
设置f = fs.GetFile(fwb)。 run time error '53' I am working on Win 7, Office 2013.
运行时错误'53'我正在使用Win 7,Office 2013。
I have an Excel spreadsheet with a filename in column A. The filenames listed in column A appear in one or more Ms office .doc files in one or more source directories. 我有一个Excel电子表格,其中A列中有一个文件名。A列中列出的文件名出现在一个或多个源目录中的一个或多个Ms office .doc文件中。
I need Excel to search the .doc files recursively and return the path(s) of the file(s) that contain the filename specified in column A into column B. If more than one file go to column C etc. 我需要Excel递归搜索.doc文件,并将包含A列中指定的文件名的文件的路径返回到B列。如果有多个文件,请转到C列,依此类推。
I am in dire need of this macro. 我非常需要这个宏。 Please some one help me.
请有人帮我。
__________________________________ __|______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
fwb = Dir(fPath & "*.*")
...
Set f = fs.GetFile(fwb) 'Run time error '53'
The Dir(...) function returns only the filename (eg "myfile.doc") so you have to add the directory path in front of it when calling GetFile(...) : Dir(...)函数仅返回文件名(例如“ myfile.doc”),因此在调用GetFile(...)时必须在其前面添加目录路径:
Set f = fs.GetFile(fPath & fwb)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.