Consulting

Results 1 to 3 of 3

Thread: Get all data from word table into excel

  1. #1

    Get all data from word table into excel

    I'm trying to get this code to extract all the table data from the word files in the folder location and put them into the same excel sheet. The part of the code in blue (or sh1.Cells(x,2)) is where i'm having trouble changing it to capture all table data not just one row of data from the word file. Thanks for any help you can give i'm a bit new to all this.

    Sub extractwordtables()
    
    Dim wrdDoc As Object, objFiles As Object, fso As Object, wordApp As Object
    Dim sh1 As Worksheet
    Dim x As Integer
    
      FolderName = "C:\code" ' Change this to the folder containing your word documents
      
      Set sh1 = ThisWorkbook.Sheets(1)
      Set fso = CreateObject("Scripting.FileSystemObject")
      Set wordApp = CreateObject("Word.application")
      Set objFiles = fso.GetFolder(FolderName).Files
      
      x = 1
      For Each wd In objFiles
          If InStr(wd, ".docx") And InStr(wd, "~") = 0 Then
          Set wrdDoc = wordApp.Documents.Open(wd.Path, ReadOnly = True)
          sh1.Cells(x, 1) = wd.Name
          sh1.Cells(x, 2) = Application.WorksheetFunction.Clean(wrdDoc.Tables(3).Cell(Row:=3, Column:=2).Range)
          
          x = x + 1
          wrdDoc.Close
        End If
        
      Next wd
      wordApp.Quit
    End Sub
    Last edited by SamT; 12-08-2014 at 10:11 AM.

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Using an Integer for a row assignment can fail.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    sub M_snb()
       sn=filter(split(createobject("wscript.shell").exec("cmd /c Dir G:\Of\*.docx /b").stdout.readall,vbcrlf),"~",false)
    
       for each it in sn
          With GetObject("G:\OF\" & it )
             .tables(1).Range.Copy
             Sheet1.Paste Sheet1.Cells(rows.count,1).end(xlup).offset(2)
             .close 0
          End With
        next
    End Sub

Tags for this Thread

Posting Permissions

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