Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-13-2023, 11:22 AM
kikola kikola is offline Extract all Word tables to Excel Windows 10 Extract all Word tables to Excel Office 2013
Novice
Extract all Word tables to Excel
 
Join Date: May 2020
Posts: 23
kikola is on a distinguished road
Default Extract all Word tables to Excel

Hello Friends, I have code for extracting word tables to excel sheets.. but it stops working after extracting some tables. Code is this:



The error alert says: Methos Pastespecial of object_Worksheet failed.

Please help...


Sub ImportWordTables()
Dim wdApp As Object
Dim wdDoc As Object
Dim wdFileName As Variant
Dim tableNo As Long
Dim tableStart As Long
Dim tableTot As Long
Dim resultRow As Long
Dim fStart As Boolean
Dim wSheet As Worksheet

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)

On Error Resume Next
Set wdApp = GetObject(Class:="Word.Application")
If wdApp Is Nothing Then
Set wdApp = CreateObject(Class:="Word.Application")
fStart = True
End If
On Error GoTo ErrHandler

Set wdDoc = wdApp.Documents.Open(Filename:=wdFileName) 'open Word file

' Repetition to force Word to calculate the correct count
tableTot = wdDoc.Tables.Count
tableTot = wdDoc.Tables.Count
tableTot = wdDoc.Tables.Count
If tableTot = 0 Then
MsgBox "This document contains no tables", vbExclamation, "Import Word Table"
ElseIf tableTot > 1 Then
tableStart = Val(InputBox("This Word document contains " & tableTot & " tables." & vbCrLf & _
"Enter the table to start from", "Import Word Table", "1"))
If tableStart < 1 Then
Beep
Exit Sub
End If
Else
tableStart = 1
End If

resultRow = 4

For tableNo = tableStart To tableTot
Set wSheet = Worksheets.Add(After:=Worksheets(Worksheets.Count) )
wSheet.Name = "Table " & tableNo
wdDoc.Tables(tableNo).Range.Copy
DoEvents
wSheet.Cells(resultRow, 1).Select
DoEvents
wSheet.PasteSpecial Format:="HTML"
Next tableNo

ExitHandler:
On Error Resume Next
wdDoc.Close SaveChanges:=False
If fStart Then
wdApp.Quit SaveChanges:=False
End If
Exit Sub

ErrHandler:
MsgBox Err.Description, vbExclamation, "Import Word Table"
Resume ExitHandler
End Sub
Reply With Quote
  #2  
Old 02-13-2023, 12:00 PM
Charles Kenyon Charles Kenyon is offline Extract all Word tables to Excel Windows 11 Extract all Word tables to Excel Office 2021
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,125
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

Can you perhaps attach a sample document that has one table where it works and one where it does not?
How to attach a screenshot or file in this forum.
Reply With Quote
  #3  
Old 02-13-2023, 12:07 PM
kikola kikola is offline Extract all Word tables to Excel Windows 10 Extract all Word tables to Excel Office 2013
Novice
Extract all Word tables to Excel
 
Join Date: May 2020
Posts: 23
kikola is on a distinguished road
Default

On this file don't works.
Attached Files
File Type: doc 1.doc (458.0 KB, 3 views)
Reply With Quote
  #4  
Old 02-13-2023, 12:08 PM
kikola kikola is offline Extract all Word tables to Excel Windows 10 Extract all Word tables to Excel Office 2013
Novice
Extract all Word tables to Excel
 
Join Date: May 2020
Posts: 23
kikola is on a distinguished road
Default

I think this is because file has line or page breaks?
Reply With Quote
  #5  
Old 02-13-2023, 12:21 PM
Charles Kenyon Charles Kenyon is offline Extract all Word tables to Excel Windows 11 Extract all Word tables to Excel Office 2021
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,125
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

Also one that does work?
Reply With Quote
  #6  
Old 02-13-2023, 12:37 PM
kikola kikola is offline Extract all Word tables to Excel Windows 10 Extract all Word tables to Excel Office 2013
Novice
Extract all Word tables to Excel
 
Join Date: May 2020
Posts: 23
kikola is on a distinguished road
Default

I dont know it worked while ago for this file but now it seems that don't works
Attached Files
File Type: docx Report.docx (283.8 KB, 1 views)
Reply With Quote
  #7  
Old 02-13-2023, 12:51 PM
Charles Kenyon Charles Kenyon is offline Extract all Word tables to Excel Windows 11 Extract all Word tables to Excel Office 2021
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,125
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

Since this seems to be a procedure started from Excel, I should move the question to the Excel Programming forum, I believe. I am in the process of doing so.
Reply With Quote
  #8  
Old 02-14-2023, 12:26 AM
kikola kikola is offline Extract all Word tables to Excel Windows 10 Extract all Word tables to Excel Office 2013
Novice
Extract all Word tables to Excel
 
Join Date: May 2020
Posts: 23
kikola is on a distinguished road
Default

No answer?
Reply With Quote
  #9  
Old 02-14-2023, 03:59 PM
Guessed's Avatar
Guessed Guessed is offline Extract all Word tables to Excel Windows 10 Extract all Word tables to Excel Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,969
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

I tested on your first doc and the macro worked fine. I did a few little fiddles to your code while I was reading through it but I don't think those alterations changed the operation in any way.

I suspect your machine may have needed a restart to clear up some memory space.
Code:
Sub ImportWordTables()
  Dim wdApp As Object, wdDoc As Object, sDocName As String
  Dim tableNo As Long, tableStart As Long, tableTot As Long, resultRow As Long, fStart As Boolean
  Dim wSheet As Worksheet
  
  sDocName = Application.GetOpenFilename("Word files (*.doc*),*.doc*", , _
              "Browse for file containing tables to be imported")
  
  If sDocName = "" Then Exit Sub '(user cancelled import file browser)
  
  On Error Resume Next
    Set wdApp = GetObject(Class:="Word.Application")
    If wdApp Is Nothing Then
      Set wdApp = CreateObject(Class:="Word.Application")
      fStart = True
    End If
  On Error GoTo ErrHandler
  
  Set wdDoc = wdApp.Documents.Open(Filename:=sDocName) 'open Word file
  
  ' Repetition to force Word to calculate the correct count
  'tableTot = wdDoc.Tables.Count
  'tableTot = wdDoc.Tables.Count
  tableTot = wdDoc.Tables.Count
  If tableTot = 0 Then
    MsgBox "This document contains no tables", vbExclamation, "Import Word Table"
  ElseIf tableTot > 1 Then
    tableStart = Val(InputBox("This Word document contains " & tableTot & " tables." & vbCrLf & _
            "Enter the table to start from", "Import Word Table", "1"))
    If tableStart < 1 Or tableTot < tableStart Then
      Beep
      Exit Sub
    End If
  Else
    tableStart = 1
  End If
  
  resultRow = 4
  
  For tableNo = tableStart To tableTot
    Set wSheet = Worksheets.Add(After:=Worksheets(Worksheets.Count))
    wSheet.Name = "Table " & tableNo
    wdDoc.Tables(tableNo).Range.Copy
    DoEvents
    wSheet.Cells(resultRow, 1).Select
    DoEvents
    wSheet.PasteSpecial Format:="HTML"
  Next tableNo
  
ExitHandler:
  On Error Resume Next
  wdDoc.Close SaveChanges:=False
  If fStart Then wdApp.Quit SaveChanges:=False
  Exit Sub
  
ErrHandler:
  MsgBox Err.Description, vbExclamation, "Import Word Table"
  Resume ExitHandler
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
batch extract all tables in multiple word documents ZaidaBa Word Tables 3 05-08-2017 10:22 PM
Extract all Word tables to Excel How to extract a word from an excel string with various lengths keywestsue Excel 3 09-18-2015 07:32 AM
Extract all Word tables to Excel Extract Excel Data from Chart in Word cillianmccolgan Word 1 08-15-2014 01:42 AM
Extract tables as images didijaba Word VBA 2 05-06-2014 06:14 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 05:52 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft