Using VBA to copy tables from Word to Excel?

Telloh

New Member
Joined
May 23, 2006
Messages
2
Hi everyone! I am currently trying to get some tables copied from word to excel. I have some code in word that will copy the tables and put it to a new excel file. However, I want it to go to sheet 2 of an excel file I already have. Any help would be appreciated. Here is what i have so far.

Sub ExtractTablesToExcel()

Dim oExcelApp As Excel.Application, oExcelWrkBook As Excel.Workbook
Dim oTbl As Table, oTbls As Tables
Dim nRows As Integer, rngPaste As Excel.Range, nPasteRow As Integer

' create excel workbook
Set oExcelApp = CreateObject("Excel.Application")
Set oExcelWrkBook = oExcelApp.Workbooks.Add
oExcelWrkBook.Application.Visible = True

nRows = 1
nPasteRow = 1
Set oTbls = ThisDocument.Tables

For Each oTbl In oTbls
oTbl.Select
Selection.Copy

Set rngPaste = oExcelWrkBook.Worksheets(1).Cells(nPasteRow, 1)
rngPaste.PasteSpecial (xlPasteValues)
nRows = oTbl.Rows.Count
nPasteRow = nPasteRow + nRows + 1

Next oTbl

oExcelWrkBook.Worksheets(1).Activate

Set oExcelApp = Nothing
Set oExcelWrkBook = Nothing
Set oTbls = Nothing

End Sub
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Hmm, came up with a solution.

Set oExcelWrkBook = oExcelApp.Workbooks.Add change to:

Set oExcelWrkBook = oExcelApp.Workbooks.Open("path/filename")

If anyone has a different idea / way I would still be glad to hear it.
 
Upvote 0

Forum statistics

Threads
1,224,905
Messages
6,181,661
Members
453,059
Latest member
jkevin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top