1
我正在創建一個Excel表格來搜索單詞(.docx)文檔的數據庫,然後我想打開它們。如果是電子表格,我可以找到該文件並將其打開。但我不能讓文檔打開?在「objWord.documents.Open xFile」一行中出現不匹配錯誤,任何答案?從Excel中搜索和打開Word文檔
Private Sub CommandButton1_Click()
Call FindFile
End Sub
Sub FindFile()
Dim xFile As File
Dim xFolder As Folder
Set Fsys = CreateObject("Scripting.FileSystemObject")
Set ObjFolder = Fsys.GetFolder("File Folder")
For Each xFolder In ObjFolder.SubFolders
If FindFiles(xFolder) = True Then Exit Sub
If FindFolder(xFolder) = True Then Exit Sub
Next
Set Fsys = Nothing
MsgBox "File not found.."
End Sub
Function FindFolder(FolderPath As Folder) As Boolean
Dim RootFolder As Folder
For Each RootFolder In FolderPath.SubFolders
If FindFiles(RootFolder) = True Then
FindFolder = True
Exit Function
End If
Call FindFolder(RootFolder)
Next
End Function
Function FindFiles(xFolder As Folder) As Boolean
Dim xFile As File
For Each xFile In xFolder.Files
FindFiles = False
If LCase(xFile.Name) = LCase(Range("B2").Value) Then
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
objWord.documents.Open xFile
FindFiles = True
FindFiles = True
Exit Function
End If
Next
End Function