2017-04-23 157 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歡迎您,很高興提供幫助。 –