+ Reply to Thread
Results 1 to 6 of 6

Macro to Export Word Tables to Excel

Hybrid View

  1. #1
    Registered User
    Join Date
    05-17-2013
    Location
    South Carolina
    MS-Off Ver
    Excel 2010
    Posts
    16

    Macro to Export Word Tables to Excel

    I have a Word document that has several tables which I would like to pull into Excel (each table on a separate sheet). I have been copying/pasting the tables, but was wondering if there is a macro that can pull them in for me.

    I found the following macro online, but when I add it as a module in Excel, I get a run-time error at the
    wdFileName = Application.GetOpenFilename("Word files (*.doc),*.doc", , _
    "Browse for file containing table to be imported")
    line.

    Does anyone know why?

    I am using Office 2010, and am on a MAC. I tried this on a PC as well and still got an error.

    Sub ImportWordTable()
    Dim wdDoc As Object
    Dim wdFileName As Variant
    Dim TableNo As Integer 'table number in Word
    Dim iRow As Long 'row index in Excel
    Dim iCol As Integer 'column index in Excel
    
    wdFileName = Application.GetOpenFilename("Word files (*.doc),*.doc", , _
    "Browse for file containing table to be imported")
    
    If wdFileName = False Then Exit Sub '(user cancelled import file browser)
    
    Set wdDoc = GetObject(wdFileName) 'open Word file
    
    With wdDoc
    TableNo = wdDoc.tables.Count
    If TableNo = 0 Then
    MsgBox "This document contains no tables", _
    vbExclamation, "Import Word Table"
    ElseIf TableNo > 1 Then
    TableNo = InputBox("This Word document contains " & TableNo & " tables." & vbCrLf & _
    "Enter table number of table to import", "Import Word Table", "1")
    End If
    With .tables(TableNo)
    'copy cell contents from Word table cells to Excel cells
    For iRow = 1 To .Rows.Count
    For iCol = 1 To .Columns.Count
    Cells(iRow, iCol) = WorksheetFunction.Clean(.cell(iRow, iCol).Range.Text)
    Next iCol
    Next iRow
    End With
    End With
    
    Set wdDoc = Nothing
    
    End Sub

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Macro to Export Word Tables to Excel

    your code works on my excel 2010 win 8, but only with doc file, not docx
    Last edited by patel45; 05-18-2013 at 08:37 AM.
    If solved remember to mark Thread as solved

  3. #3
    Registered User
    Join Date
    05-17-2013
    Location
    South Carolina
    MS-Off Ver
    Excel 2010
    Posts
    16

    Re: Macro to Export Word Tables to Excel

    Yes, the file I want to import is a .doc file.
    I found a forum that said sometimes you have to replace the quotes as they don't come across correctly when copied. That didn't fix it, but it did highlight that the underscore in this line was "invalid character."

    wdFileName = Application.GetOpenFilename("Word files (*.doc),*.doc", ,  _ "Browse for file containing table to be imported")
    I tried deleting the underscore and also tried adding a file name
    wdFileName = Application.GetOpenFilename("Word files (*.doc),*.doc", "Test.doc", "Browse for file containing table to be imported")
    but in both cases I get a "type mismatch" error on the following line:
    If wdFileName = False Then Exit Sub '(user cancelled import file browser)
    I know it's something I'm not doing correctly, but I'm not sure what it is. . .

  4. #4
    Registered User
    Join Date
    11-09-2009
    Location
    london, england
    MS-Off Ver
    Excel 2010
    Posts
    52

    Re: Macro to Export Word Tables to Excel

    Hi WesternGal

    I too gave your original code a go using Excel 2010 on Windows 7 and it worked for both doc and docx files. I was unable to replicate the error.
    However, I had a quick look online and found this useful msdn resource that describes "Programmatically Selecting Files in Excel for Windows and Excel for the Mac" http://msdn.microsoft.com/en-us/libr...FileNameMethod

    Hope the msdn link is of some help to you.
    Please click * if the answer was helpful.

  5. #5
    Registered User
    Join Date
    05-17-2013
    Location
    South Carolina
    MS-Off Ver
    Excel 2010
    Posts
    16

    Re: Macro to Export Word Tables to Excel

    Found an answer: on a MAC, the line has to read:
    wdFileName = Application.GetOpenFilename( )
    There were multiple other errors in the code I had copied, but I found the correct text to loop through all tables in Word and import them to excel on this thread:
    HTML Code: 
    Sub ImportWordTable3()
    'Import all tables to a single sheet
    Dim wdDoc As Object
    Dim wdFileName As Variant
    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
    
    wdFileName = Application.GetOpenFilename()
    
    If wdFileName = False Then Exit Sub '(user cancelled import file browser)
    
    Set wdDoc = GetObject(wdFileName) 'open Word file
    
    With wdDoc
        If wdDoc.tables.Count = 0 Then
            MsgBox "This document contains no tables", _
                vbExclamation, "Import Word Table"
        Else
            jRow = 0
            Sheets.Add after:=Sheets(Worksheets.Count)
            For TableNo = 1 To wdDoc.tables.Count
                With .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
    End With
    Set wdDoc = Nothing
    End Sub

  6. #6
    Registered User
    Join Date
    11-23-2014
    Location
    Switzerland
    MS-Off Ver
    Word, Excel, Outlook, Power Point - all 2010
    Posts
    1

    Re: Macro to Export Word Tables to Excel

    Thanks a million, Western Gal. I had searched for days and worked sooo many hours to try to solve this problem. With your code plus a couple of tiny twiddles to it to import data from just one single-line table, it ran perfectly on my Windows PC.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1