[英]How to copy first and last row of each table of Word file to an Excel sheet?
There are around 150 tables in my Word file.我的 Word 文件中有大约 150 个表。 Each table has six rows and two columns.
每个表有六行和两列。
I need to copy the first and last row values from each table to an Excel sheet.我需要将每个表中的第一行和最后一行值复制到 Excel 工作表中。
The first row will have my table id and last row data is Pass/Fail.第一行将有我的表 ID,最后一行数据是通过/失败。
In general, you should try to post the code that you tried so far so we can help you debug and get to the final answer:通常,您应该尝试发布到目前为止您尝试过的代码,以便我们可以帮助您调试并获得最终答案:
But, I'll try to give you some code to get going.但是,我会尝试给你一些代码来开始。
If it helps you, please consider to mark it as the correct answer :)如果对您有帮助,请考虑将其标记为正确答案:)
Please try the following code:请尝试以下代码:
Sub extract_word_table_values_to_excel()
Dim word_app As Object, temp_doc As Object, word_doc As Object
'Set word_app = CreateObject("Word.Application")
ActiveSheet.Calculate
word_path = Range("Word_path")
'Place your word doc path here in the named range on the sheet
temp = Split(word_path, "\")
word_name = temp(UBound(temp))
Set word_doc = GetObject(word_path)
Set word_app = word_doc.Application
word_app.Visible = True
word_doc.Activate
excel_row = 1
On Error Resume Next
Dim word_table As Word.Table 'Or As variant
For Each word_table In word_doc.Tables
Err.Clear
For i = 1 To word_table.Rows.Count Step word_table.Rows.Count - 1
'Step count minus 1 means only first and last row are looped
For j = 1 To word_table.Columns.Count
part = word_table.Rows(i).Cells(j).Range.Text
part = Left(part, Len(part) - 1)
part = Replace(part, vbNewLine, "")
'MsgBox part
Sheet2.Cells(excel_row, j).Value = part
Next j
excel_row = excel_row + 1
Next i
excel_row = excel_row + 1 'Leave row between tables
Next word_table
MsgBox "done"
word_doc.Save
'word_app.Quit
Set word_doc = Nothing
Set word_app = Nothing
End Sub
Please leave a comment if you have a question.如果您有问题,请发表评论。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.