简体   繁体   English

如何将Word文件每个表格的第一行和最后一行复制到Excel工作表?

[英]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:通常,您应该尝试发布到目前为止您尝试过的代码,以便我们可以帮助您调试并获得最终答案:

  1. Summarize the problem总结问题
  2. Describe what you've tried描述你尝试过的
  3. Show some code When appropriate, share the minimum amount of code others need to reproduce your problem (also called a minimum, reproducible example )显示一些代码 适当时,共享其他人重现您的问题所需的最少代码(也称为最小的、可重现的示例

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.

相关问题 如何将最后一行复制到另一个Excel工作表? - how to copy last row to another excel sheet? 想要将多个Excel文件的每张纸的第一行复制到另一个Excel文件 - Want to copy first row of each sheet of multiple Excel files to another Excel file Excel VBA如果第一行包含单词“结束日期”,则将工作表1的粘贴列复制到工作表2 - Excel VBA Copy Paste column from sheet 1 to sheet 2 if first row countain the word “End Date” 为Excel工作表中的每一行创建Word表 - Create tables in word for each row in excel sheet 将Excel工作表复制到Word - copy Excel sheet to Word 如何使用PowerShell将txt文件中每行的第一个单词放入Excel工作表中? - How do I take the first word from each line in txt file and put them into an Excel sheet using PowerShell? 如何使用excel vba自动复制同一工作表中最后一行的数据并将其复制到同一表的最后一行? - How to copy the data in last row and copy to the last row of a table in the same worksheet automatically using excel vba? Excel VBA:如何将值粘贴到表的最后一行的第一个单元格中? - Excel VBA: How to paste a value in the first Cell of the last row of a table? 从记事本复制并粘贴到 Excel 工作表的最后一行 - Copy and paste to the last row of Excel sheet from Notepad 将未打开的excel工作表中的excel数据复制并粘贴到目标excel工作表到最后一行 - Copy and Pasting excel data from unopened excel sheet to destination excel sheet to the last row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM