Excel to Word macro using tables...

I have a working macro in Excel to copy a table and paste it to a bookmark in word. The user wants to be able to keep the first row of the table (header) showing if the result of the paste makes the table span more than one page. For example, the table in excel is

Hdr Hdr Hdr Hdr

1    2       3      4

1    2       3      4

1    2       3      4

but after the 2nd row it goes to a new page. So the user would like to see the resultant Word table look like

 

Hdr Hdr Hdr Hdr

1    2       3      4

Hdr Hdr Hdr Hdr

1    2       3      4

1    2       3      4

 

Can this be accomplished from within the Excel macro?

 

(My guess is no).

 

Bob Umlas (Excel MVP)

 

Thanks.

Bob;

I had this problem a while ago. This was my solution.

Create a table with a header row and one data row. Specify the header row to Repeat as header row at the top of each page within Table ► Properties. I actually made a table style for this as there were numerous tables with the same basic layout and formatting requirements. Bookmark the (almost) empty table. To keep things simple, I used identically named Word bookmarks and Excel named ranges each with rpt_ as an identifying prefix.

From Excel, my macro opened the Word document and fed the data in cell by cell. This was more time intensive than copying and pasting the table as a whole but it isn't too bad if the Word document is hidden. If it isn't hidden, you can watch the Word table calls populate. Something like,

''Tables transfer
With ThisWorkbook
    For Each nm In .Names
        If Left(nm.Name, 4) = "rpt_" Then
            'Debug.Print nm.Name
            With .Names(nm.Name).RefersToRange
                For x = 1 To .Rows.Count  'change to x=2 if the Word headers are OK
                    If x > wdDOCX.Bookmarks(nm.Name).Range.Tables(1).Rows.Count Then
                        wdDOCX.Bookmarks(nm.Name).Range.Tables(1).Rows.Add
                    End If
                    For y = 1 To .Columns.Count
                        wdDOCX.Bookmarks(nm.Name).Range.Tables(1).Cell(x, y).Range.Text = .Cells(x, y).Text
                    Next y
                Next x
            End With
        End If
    Next nm
End With

You could probably just modify the Word table's header row after the wholesale table paste for to Repeat as header row at the top of each page but this is how I brought in a few dozen data tables to a new Word document based upon a custom template. As this was a small part of a report generation routine that took a couple of hours, time was not mission critical.

I chopped up and redacted the code. I hope it's mostly usable.

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Normally I would say this is a valid solution, but the Word document has no tables, just bookmarks for them. So I can't mark their headers to repeat as header row at the top of each page. Also, since this is an 85-100 page document with many tables, time is an issue... cell by cell would not be OK.

 

Any other ideas?

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated October 5, 2021 Views 627 Applies to: