PDA

View Full Version : VBA Copy Tables from Word to Excel



muhammad susanto
02-16-2022, 09:15 PM
Cross posted
http://www.eileenslounge.com/viewtopic.php?f=26&t=37822




hi all.

i found code from google but i don't how the code is worked well, i have testing but not work
i want the code can copy table from ms word into ms excel

Sub CopyTables()
Dim oWord As Word.Application
Dim WordNotOpen As Boolean
Dim oDoc As Word.Document
Dim oTbl As Word.Table
Dim fd As Office.FileDialog
Dim FilePath As String
Dim wbk As Workbook
Dim wsh As Worksheet

' Prompt for document
Set fd = Application.FileDialog(msoFileDialogOpen)
With fd
.Filters.Clear
.Filters.Add "Word Documents (*.docx)", "*.docx", 1
.Title = "Choose a Word File"
If .Show = True Then
FilePath = .SelectedItems(1)
Else
Beep
Exit Sub
End If
End With

On Error Resume Next

Application.ScreenUpdating = False

' Create new workbook
Set wbk = Workbooks.Add(Template:=xlWBATWorksheet)

' Get or start Word
Set oWord = GetObject(Class:="Word.Application")
If Err Then
Set oWord = New Word.Application
WordNotOpen = True
End If

On Error GoTo Err_Handler

' Open document
Set oDoc = oWord.Documents.Open(Filename:=FilePath)
' Loop through the tables
For Each oTbl In oDoc.Tables
' Create new sheet
Set wsh = wbk.Worksheets.Add(After:=wbk.Worksheets(wbk.Works heets.Count))
' Copy/paste the table
oTbl.Range.Copy
wsh.Paste
Next oTbl

' Delete the first sheet
Application.DisplayAlerts = False
wbk.Worksheets(1).Delete
Application.DisplayAlerts = True

Exit_Handler:
On Error Resume Next
oDoc.Close SaveChanges:=False
If WordNotOpen Then
oWord.Quit
End If
'Release object references
Set oTbl = Nothing
Set oDoc = Nothing
Set oWord = Nothing
Application.ScreenUpdating = True
Exit Sub

Err_Handler:
MsgBox "Word caused a problem. " & Err.Description, vbCritical, "Error: " & Err.Number
Resume Exit_Handler
End Sub

this original link
https://answers.microsoft.com/en-us/msoffice/forum/all/how-to-copy-tables-from-word-to-excel-using-vba/e521e458-0f2a-4b40-bffd-c9a981636d24

anyone help me out..greatly appreciated
.susanto

DocAElstein
02-16-2022, 10:02 PM
Hi
I can’t help directly as I have little experience with Microsoft Word.
We do not have many Word VBA experts looking in to excelfox

However, I can tell you that the Author of that code, HansV, is quite active posting at Eileen’s Lounge, https://eileenslounge.com/app.php/portal

Here is the link to the Word Sub Forum there:
https://eileenslounge.com/viewforum.php?f=26

You may be able to get help from that forum, if you join and post your question there
If you decide to ask for help there, then I would suggest that you prepare and upload a Word File document example, and explain clearly exactly what you want the coding to do.

( Remember to tell them that you have also posted here:
You must tell them, that you have also posted the same question at excelfox: Give them this link to your post here:
https://excelfox.com/forum/showthread.php/2786-VBA-Copy-Tables-from-Word-to-Excel )



Alan