循环遍历目录中的Word文档,提取文本,并将其追加到Excel文件中

huangapple go评论53阅读模式
英文:

Loop through Word docs in a directory, extract text, and append it to an Excel file

问题

我想从目录中的每个Word文档中提取一个分数,然后将该分数附加到Excel工作簿。

什么都没有发生。没有出现错误。
脚本似乎起作用并打印了文件名。因为我注意到有些问题,所以我取消了该过程。从那以后脚本就没有起作用了。

Sub DataExtraction()
Dim StrFile As String
StrFile = Dir("C:\Users\lones\Desktop\Business Documents")
Do While Len(StrFile) > 0
    Debug.Print StrFile
    StrFile = Dir
    Dim wdApp As Word.Application
    Dim wDoc As Word.Document
    Dim wRng As Word.Range
    Dim rngTest As Word.Range
    Dim rngEnd As Word.Range
    Dim strTheText As String
    Set wdApp = New Word.Application
    wdApp.Visible = True
    Const ExcelFile As String = "C:\Users\lones\Desktop\Business Data Extraction.xlsm"
    Set wDoc = wdApp.Documents.Open(Filename:=StrFile, ReadOnly:=True, AddToRecentfiles:=False)
    Set rngTest = wDoc.Range
    If rngTest.Find.Execute(FindText:="Test description... This user had ") Then
        Set rngEnd = wDoc.Range(rngTest.End, wDoc.Range.End)
        If rngEnd.Find.Execute(FindText:=" correct answers") Then
            Set wRng = wDoc.Range(rngTest.End, rngEnd.Start)
            wRng.Copy
        End If
    End If
    Dim LastRow As Long
    LastRow = Cells(Rows.Count, 1).End(xlUp).Row
    Debug.Print CStr(LastRow)
    Range("A" & CStr(LastRow)).Select
    ActiveWorkbook.ActiveSheet.Paste
Loop
End Sub
英文:

I want to extract a score from each Word document in a directory then append that score to an Excel workbook.

Nothing happens. No error is thrown.
The script did seem to work and printed the file name. I cancelled the process because I noticed something was wrong. The script hasn't worked since.

Sub DataExtraction()
Dim StrFile As String
StrFile = Dir("C:\Users\lones\Desktop\Business Documents")
Do While Len(StrFile) > 0
    Debug.Print StrFile
    StrFile = Dir
    Dim wdApp As Word.Application
    Dim wDoc As Word.Document
    Dim wRng As Word.Range
    Dim rngTest As Word.Range
    Dim rngEnd As Word.Range
    Dim strTheText As String
    Set wdApp = New Word.Application
    wdApp.Visible = True
    Const ExcelFile As String = "C:\Users\lones\Desktop\Business Data Extraction.xlsm"
    Set wDoc = wdApp.Documents.Open(Filename:=StrFile, ReadOnly:=True, AddToRecentfiles:=False)
    Set rngTest = wDoc.Range
    If rngTest.Find.Execute(FindText:="Test description... This user had ") Then
        Set rngEnd = wDoc.Range(rngTest.End, wDoc.Range.End)
        If rngEnd.Find.Execute(FindText:=" correct answers") Then
            Set wRng = wDoc.Range(rngTest.End, rngEnd.Start)
            wRng.Copy
        End If
    End If
    Dim LastRow As Long
    LastRow = Cells(Rows.Count, 1).End(xlUp).Row
    Debug.Print CStr(LastRow)
    Range("A" & CStr(LastRow)).Select
    ActiveWorkbook.ActiveSheet.Paste
Loop
End Sub

答案1

得分: 0

以下是您要的翻译部分:

(would be mess as a comment)

I can't say your rest of code is correct but that Dir() looks weird to me (I am not a VBA guy, nor a VB but anyone may need to write some VBA unfortunately). Would you change that a bit:

Sub DataExtraction()
Dim StrFile As String
Dim Folder As String
Folder = "C:\Users\lones\Desktop\Business Documents"
StrFile = Dir(Folder + "*.docx")
Do While Len(StrFile) > 0
    Debug.Print StrFile

    Dim wdApp As Word.Application
    Dim wDoc As Word.Document
    Dim wRng As Word.Range
    Dim rngTest As Word.Range
    Dim rngEnd As Word.Range
    Dim strTheText As String
    Set wdApp = New Word.Application
    wdApp.Visible = True
    Const ExcelFile As String = "C:\Users\lones\Desktop\Business Data Extraction.xlsm"
    Set wDoc = wdApp.Documents.Open(Filename:=Folder + StrFile, ReadOnly:=True, AddToRecentfiles:=False)
    Set rngTest = wDoc.Range
    If rngTest.Find.Execute(FindText:="Test description... This user had ") Then
        Set rngEnd = wDoc.Range(rngTest.End, wDoc.Range.End)
        If rngEnd.Find.Execute(FindText:=" correct answers") Then
            Set wRng = wDoc.Range(rngTest.End, rngEnd.Start)
            wRng.Copy
        End If
    End If
    Dim LastRow As Long
    LastRow = Cells(Rows.Count, 1).End(xlUp).Row
    Debug.Print CStr(LastRow)
    Range("A" & CStr(LastRow)).Select
    ActiveWorkbook.ActiveSheet.Paste
    StrFile = Dir
Loop
End Sub

希望这个翻译对您有所帮助。如果您有任何其他问题,请随时提出。

英文:

(would be mess as a comment)

I can't say your rest of code is correct but that Dir() looks weird to me (I am not a VBA guy, nor a VB but anyone may need to write some VBA unfortunately). Would you change that a bit:

Sub DataExtraction()
Dim StrFile As String
Dim Folder As String
Folder = "C:\Users\lones\Desktop\Business Documents"
StrFile = Dir(Folder + "*.docx")
Do While Len(StrFile) > 0
    Debug.Print StrFile
    
    Dim wdApp As Word.Application
    Dim wDoc As Word.Document
    Dim wRng As Word.Range
    Dim rngTest As Word.Range
    Dim rngEnd As Word.Range
    Dim strTheText As String
    Set wdApp = New Word.Application
    wdApp.Visible = True
    Const ExcelFile As String = "C:\Users\lones\Desktop\Business Data Extraction.xlsm"
    Set wDoc = wdApp.Documents.Open(Filename:=Folder + StrFile, ReadOnly:=True, AddToRecentfiles:=False)
    Set rngTest = wDoc.Range
    If rngTest.Find.Execute(FindText:="Test description... This user had ") Then
        Set rngEnd = wDoc.Range(rngTest.End, wDoc.Range.End)
        If rngEnd.Find.Execute(FindText:=" correct answers") Then
            Set wRng = wDoc.Range(rngTest.End, rngEnd.Start)
            wRng.Copy
        End If
    End If
    Dim LastRow As Long
    LastRow = Cells(Rows.Count, 1).End(xlUp).Row
    Debug.Print CStr(LastRow)
    Range("A" & CStr(LastRow)).Select
    ActiveWorkbook.ActiveSheet.Paste
    StrFile = Dir
Loop
End Sub

huangapple
  • 本文由 发表于 2023年2月27日 07:21:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75575613.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定