VBA:提取特定的Word表格到Excel

kokeuurv  于 2023-05-19  发布在  其他
关注(0)|答案(1)|浏览(212)

编辑:所以我设法识别所有th字文档。然而,我有一个问题,以实现提取第二列行2的数据表。
错误:设置表格= doc.表格对象变量块
单词表看起来像附加的img。我只想提取从行2列2开始的“x”。
希望有人能帮我这个忙。提前感谢!

Sub Open_Multiple_Word_Files(sPattern As String)
Dim shApp As Object
Dim shFolder As Object
Dim File, Files
Dim currentPath As String
Dim doc As Word.Document
Dim wd As New Word.Application
Dim sh As Worksheet

'get folder
    Set shApp = CreateObject("shell.application")
    folder2search = "C:\Users\ChrisLacs\Desktop\extrac\"
    Set shFolder = shApp.Namespace(folder2search)
'get files
    Set Files = shFolder.Items()
'check if file names match the pattern
    For Each File In Files
        If File.Name Like sPattern Then
        
        
           

Set Table = doc.Table
Set sh = ActiveSheet

lr = sh.Cells(Rows.Count, 1).End(xlUp).Row + 1

For i = 1 To 6
    sh.Cells(lr, i).Value = Application.WorksheetFunction.Clean(Table(1).Rows(i).Cells(2).Range.Text)
    
            
            Debug.Print File.Name & " - " & (File.Name Like sPattern)
            Next
        End If
    Next File
End Sub

Sub Search_it()
    Open_Multiple_Word_Files "Form*"
End Sub
wlsrxk51

wlsrxk511#

您的代码只打开一个word文件,因为这行:
Set doc = wd.Documents.Open(ActiveWorkbook.Path & "\Form.docx")
只打开您指定的文件。
打开特定文件夹中匹配模式Form*的所有word文件的一种方法如下:

Sub Open_Multiple_Word_Files(sPattern As String)
Dim shApp As Object
Dim shFolder As Object
Dim File, Files
Dim currentPath As String
'get folder
    Set shApp = CreateObject("shell.application")
    folder2search = "C:\Users\...\Documents"
    Set shFolder = shApp.Namespace(folder2search)
'get files
    Set Files = shFolder.Items()
'check if file names match the pattern
    For Each File In Files
        If File.Name Like sPattern Then
            'write here things to do when pattern s matched.
            'Debug.Print File.Name & " - " & (File.Name Like sPattern)
        End If
    Next File
End Sub

Sub Search_it()
    Open_Multiple_Word_Files "Form*"
End Sub

相关问题