2017-04-23 56 views
4

我需要从word文档的页脚部分获取内容。特别是我需要将页脚部分内的表格内容转移到Excel文档中。我已经使用word文档正文部分的表格来完成此操作。如何将页脚部分的内容放入其他工作表中,或者将其与文档正文的内容一起添加到现有工作表中?vba从word文档中读取页脚的内容

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 

回答

4

为了得到一个表中单词的页脚部分,使用:

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

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

PS。 Footers(1) = Footers(wdHeaderFooterPrimary)但是,当您使用后期绑定来驱动单词时,您没有定义此常量。

+0

非常感谢你,这对我帮助很大。 –

+0

@ tim.w欢迎您,很高兴提供帮助。 –