Copying word tableObjects to excelRange in VBA fails

VBAneophite22

New Member
Joined
Dec 30, 2024
Messages
2
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hi all,

What I want: copy some tables from a word document to Excel, copying them to 1 worksheet and just add some enters between each copied result.

What I tried: what works is if i use a new worksheet Object to .Paste to. But if use a Range object somehow both the .Paste and the .PasteSpecial methods fail.

I am not used to working with word table objects... but I can't figure it out. Here is an example that just requires some word document to be open with some table in it.


Code snippet

VBA Code:
Sub ExportTablesToExcel_test()
    Dim wdApp As Object
    Dim wdDoc As Object
    Dim wdTable As Object
    Dim xlApp As Object
    Dim xlWB As Object
    Dim xlWS As Object
    Dim rowOffset As Long
    Dim tableIndex As Long
    Dim tableRows As Long
    Dim tableCols As Long
    Dim targetRange As Range

    Set xlApp = CreateObject("Excel.Application")
    Set xlWB = xlApp.Workbooks.Add
    Set xlWS = xlWB.Worksheets(1) ' Use the first worksheet
   

    Set wdApp = GetObject(, "Word.Application")
    Set wdDoc = wdApp.ActiveDocument

    rowOffset = 1
   
    ' Loop through all tables in the Word document
    For tableIndex = 1 To wdDoc.Tables.Count
        Set wdTable = wdDoc.Tables(tableIndex)
       
        ' Get the number of rows and columns in the Word table
        tableRows = wdTable.Rows.Count
        tableCols = wdTable.Columns.Count
        wdTable.Range.Select
       
        ' Copy the table content from Word
        wdTable.Range.Copy
       

        '< does work, but not what i want >
        xlWS.Paste
       
                '< does not work >
        ' Resize the target range in Excel to match the size of the Word table
        Set targetRange = xlWS.Cells(rowOffset, 1).Resize(tableRows, tableCols)
        targetRange.PasteSpecial xlPasteValues
   
       
        ' Adjust rowOffset to leave 2 empty rows after each table
        rowOffset = xlWS.Cells(xlWS.Rows.Count, 1).End(xlUp).Row + 3 ' 2 empty rows after the table
       
    Next tableIndex
   
    ' Cleanup
'    wdDoc.Close False ' Close the Word document without saving
'    wdApp.Quit ' Quit the Word application
    Set wdTable = Nothing
    Set wdDoc = Nothing
    Set wdApp = Nothing
    Set xlWS = Nothing
    Set xlWB = Nothing
    Set xlApp = Nothing

End Sub
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Well I found the issue. I was referencing a Range Object that is either an Microsoft Word or Excel Reference Object.

So for others new to working with multiple objects like an Excel or a Word object. The fix;
- import direct variables referencing the correct library. This prevents vague "object not found" errors. So don't do dim targetRange as Object, but do: dim targetRange as Excel.Range
- if this naming .word / .excel is not working - you have to import the correct VBA reference library
 
Upvote 0
Solution

Forum statistics

Threads
1,225,204
Messages
6,183,562
Members
453,169
Latest member
Marlon18

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