Results 1 to 9 of 9

Thread: summarize multiple word tables to excel

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #8
    VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,273
    Location
    Try the following Excel macro:
    Sub TallyTableData()
    'Note: this code requires a reference to the Word object model.
    'See under the VBE's Tools|References.
    Application.ScreenUpdating = False
    Dim wdApp As New Word.Application, wdDoc As Word.Document, wdRng As Word.Range
    Dim strFolder As String, strFile As String
    Dim WkSht As Worksheet, r As Long
    strFolder = GetFolder: If strFolder = "" Then GoTo ErrExit
    Set WkSht = ActiveSheet: r = 1
    strFile = Dir(strFolder & "\*.doc", vbNormal)
    While strFile <> ""
      Set wdDoc = wdApp.Documents.Open(Filename:=strFolder & "\" & strFile, AddToRecentFiles:=False, ReadOnly:=True, Visible:=False)
      With wdDoc
        If .Tables.Count > 1 Then
          Set wdRng = .Tables(2).Range
          If r > 1 Then wdRng.Start = wdRng.Tables(1).Rows(2).Range.Start
          wdRng.Copy
          WkSht.Paste Destination:=WkSht.Range("A" & r)
          r = r + wdRng.Rows.Count + 1
         End If
        .Close SaveChanges:=False
      End With
      strFile = Dir()
    Wend
    With WkSht
      .UsedRange.Sort Key1:=.Columns("A"), Order1:=xlAscending, Header:=xlNo
      For r = .Cells(.Rows.Count, 1).End(xlUp).Row To 2 Step -1
        If .Range("A" & r - 1).Value = .Range("A" & r).Value Then
          If IsNumeric(.Range("B" & r).Value) Then .Range("B" & r - 1).Value = .Range("B" & r).Value + .Range("B" & r - 1).Value
          .Rows(r).EntireRow.Delete
        End If
      Next
    End With
    ErrExit:
    wdApp.Quit
    Set wdRng = Nothing: Set wdDoc = Nothing: Set wdApp = Nothing: Set WkSht = Nothing: Set WkBk = Nothing
    Application.ScreenUpdating = True
    End Sub
     
    Function GetFolder() As String
        Dim oFolder As Object
        GetFolder = ""
        Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
        If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
        Set oFolder = Nothing
    End Function
    The macro has a folder browser, so all you need do is select the folder to process.
    Last edited by macropod; 04-15-2018 at 04:06 PM. Reason: coding error fixed
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •