Excel宏:循环到文档表而不是页面时,如何获取word标题

Excel宏:循环到文档表而不是页面时,如何获取word标题,excel,vba,Excel,Vba,我有一个文档,它通过excel宏获取word文档中的所有表,并循环每个表以获取数据并将其转换为excel。问题是我需要在每个页面的页眉内提取一个数据(字符串),同时循环到表中,以将表数据与其员工关联 示例标题值: Date | No. of Tardy | No. of Undertime 11/12 | 5 | 10 Name | Date | No. of Tardy | No. of Undertime Jane Motts |

我有一个文档,它通过excel宏获取word文档中的所有表,并循环每个表以获取数据并将其转换为excel。问题是我需要在每个页面的页眉内提取一个数据(字符串),同时循环到表中,以将表数据与其员工关联

示例标题值:

Date  | No. of Tardy | No. of Undertime
11/12 |      5       |      10 
Name         |  Date  | No. of Tardy | No. of Undertime
Jane Motts   |  1/12  |      1       |      6 
Jane Motts   |  2/12  |      2       |      7 
Jane Motts   |  3/12  |      3       |      8 
Clark Wright |  1/12  |      4       |      6 
Sam Molds    |  2/12  |      7       |      7 
Sam Molds    |  3/12  |      8       |      8 
第1页:“员工:简·莫茨”

第2页:“员工:简·莫茨”

第3页:“员工:克拉克·赖特”

第4页:“员工:山姆模具”等

样本表值:

Date  | No. of Tardy | No. of Undertime
11/12 |      5       |      10 
Name         |  Date  | No. of Tardy | No. of Undertime
Jane Motts   |  1/12  |      1       |      6 
Jane Motts   |  2/12  |      2       |      7 
Jane Motts   |  3/12  |      3       |      8 
Clark Wright |  1/12  |      4       |      6 
Sam Molds    |  2/12  |      7       |      7 
Sam Molds    |  3/12  |      8       |      8 
我可以成功地将此word表格数据导入excel,问题是我需要员工姓名,如下所示:

所需的导入输出:

Date  | No. of Tardy | No. of Undertime
11/12 |      5       |      10 
Name         |  Date  | No. of Tardy | No. of Undertime
Jane Motts   |  1/12  |      1       |      6 
Jane Motts   |  2/12  |      2       |      7 
Jane Motts   |  3/12  |      3       |      8 
Clark Wright |  1/12  |      4       |      6 
Sam Molds    |  2/12  |      7       |      7 
Sam Molds    |  3/12  |      8       |      8 
我的ImportWordTable宏如下所示:

Option Explicit

Sub ImportWordTable()

Dim wdDoc As Object
Dim wdFileName As Variant
Dim tableNo As Integer 'table number in Word
Dim iRow As Long 'row index in Excel
Dim iCol As Integer 'column index in Excel
Dim resultRow As Long
Dim tableStart As Integer
Dim tableTot As Integer

On Error Resume Next

ActiveSheet.Range("A:AZ").ClearContents

wdFileName = Application.GetOpenFilename("Word files (*.docx),*.docx", , _
"Browse for file containing table to be imported")

If wdFileName = False Then Exit Sub '(user cancelled import file browser)

Set wdDoc = GetObject(wdFileName) 'open Word file

With wdDoc
    tableNo = wdDoc.tables.Count
    tableTot = wdDoc.tables.Count
    If tableNo = 0 Then
        MsgBox "This document contains no tables", _
        vbExclamation, "Import Word Table"
    ElseIf tableNo > 1 Then
        tableNo = InputBox("This Word document contains " & tableNo & " tables." & vbCrLf & _
        "Enter the table to start from", "Import Word Table", tableNo)
    End If

    resultRow = 4

    For tableStart = 1 To tableTot
        With .tables(tableStart)
            'copy cell contents from Word table cells to Excel cells
            For iRow = 1 To .Rows.Count
                For iCol = 1 To .Columns.Count
                    Cells(resultRow, iCol) = WorksheetFunction.Clean(.cell(iRow, iCol).Range.Text)
                Next iCol
                resultRow = resultRow + 1
            Next iRow
        End With
        resultRow = resultRow + 1
    Next tableStart
End With

End Sub

你试过单步遍历代码以查看循环是否引用了第一列吗?@SamuelEverson我想的是,因为我正在遍历整个文档的所有表,如果我能确定我正在阅读的当前表是哪个页面,那么我就可以访问该页面header@SamuelEverson我在想的是,因为我在循环浏览整个文件的所有表格,如果我能确定我正在阅读的当前表是哪一页,那么我就可以访问我误解了名称所在的页面标题。对不起!该对象可能有用(虽然抱歉,但我以前没有使用过),否则您可以使用该对象获得标题详细信息。如果您需要,我可以添加一些示例代码,以便在我使用电脑后将书签文本从word导入excel。让我知道。你试过单步遍历代码以查看循环是否引用了第一列吗?@SamuelEverson我想的是,因为我正在遍历整个文档的所有表,如果我能确定我正在阅读的当前表是哪个页面,那么我就可以访问该页面header@SamuelEverson我在想的是,因为我在循环浏览整个文件的所有表格,如果我能确定我正在阅读的当前表是哪一页,那么我就可以访问我误解了名称所在的页面标题。对不起!该对象可能有用(虽然抱歉,但我以前没有使用过),否则您可以使用该对象获得标题详细信息。如果您需要,我可以添加一些示例代码,以便在我使用电脑后将书签文本从word导入excel。让我知道。