vba 从word文档中读取页脚的内容

vba read the content of a footer out of a word document

我需要从 word 文档的页脚部分获取内容。特别是我需要将页脚部分内的 table 的内容传输到 excel 文档中。我已经使用 word 文档正文部分的 tables 来执行此操作。如何将页脚部分的内容添加到其他工作表中或与文档正文内容一起添加到现有工作表中?

    Sub ImportWordTable()
    Dim sPfad As String
    Dim appWord As Object
    Dim strDatei As String
    Dim TableNo As Integer 'table number in Word
    Dim iRow As Long 'row index in Word
    Dim jRow As Long 'row index in Excel
    Dim iCol As Integer 'column index in Excel
    sPfad = "C:\Users\tim\Test2\"    '<== adjust path
    Application.ScreenUpdating = False
    Set appWord = CreateObject("Word.Application")
    appWord.Visible = True
    strDatei = Dir(sPfad & "*.doc*")
    Do While strDatei <> ""
        appWord.Documents.Open sPfad & strDatei
        'Read all tables of the document body
        If appWord.ActiveDocument.tables.Count = 0 Then
            MsgBox "This document contains no tables", _
                vbExclamation, "Import Word Table"
        Else
            jRow = 0
            Sheets.Add after:=Sheets(Worksheets.Count)
            ActiveSheet.Name = strDatei & "Label-Text"
            For TableNo = 1 To appWord.ActiveDocument.tables.Count
                With appWord.ActiveDocument.tables(TableNo)
    'copy cell contents from Word table cells to Excel cells
                    For iRow = 1 To .Rows.Count
                        jRow = jRow + 1
                        For iCol = 1 To .Columns.Count
                            On Error Resume Next
                            ActiveSheet.Cells(jRow, iCol) = WorksheetFunction.Clean(.cell(iRow, iCol).Range.Text)
                            On Error GoTo 0
                        Next iCol
                    Next iRow
                End With
                jRow = jRow + 1
            Next TableNo
        End If
        appWord.ActiveDocument.Close savechanges:=False
        strDatei = Dir
    Loop
    appWord.Quit
    Set appWord = Nothing
End Sub

要在单词的 页脚 部分获得 table,请使用:

appWord.ActiveDocument.Sections(1).Footers(1).Range.Tables.Count

With appWord.ActiveDocument.Sections(1).Footers(1).Range.Tables(TableNo)

ps。 Footers(1) = Footers(wdHeaderFooterPrimary) 但是在使用后期绑定驱动单词时没有定义这个常量。