Solved

Inserting Page Breaks into MS Word VBA

Posted on 2013-11-26
7
1,611 Views
Last Modified: 2013-12-23
I have a VBA macro that copies a table from MS Excel and pastes the contents into MS Word using bookmarks. The size of the table length varies greatly and sometimes takes up multiple pages.

Is there a way to get VBA to automatically 'break' the table whenever it should roll over onto a new page? Currently, the entire table resides on one page so much of the content doesn't fit and ends up getting effectively hidden.

Sheets("Customer Quotes").Select
ActiveSheet.Cells(1, 1).Activate
ActiveCell.SpecialCells(xlLastCell).Select
Lastrow = ActiveCell.Row
table_range = "A1" & ":" & "D" & Lastrow
ActiveSheet.Range(table_range).Select
Selection.Copy


Set rng = wrdApp.ActiveDocument.Bookmarks("Chart").Range
rng.Paste
Set tbl = rng.Tables(1)
With tbl.Rows
        .RelativeHorizontalPosition = wdRelativeHorizontalPositionMargin
        .HorizontalPosition = wdTableCenter
End With

Open in new window

Thanks!
0
Comment
Question by:DC1983
7 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39678822
I can't reproduce that effect. In fact I don't know any way to keep overflow rows on the same page as the start of the table.

Can you post a sample workbook and the output document, please?
0
 
LVL 49

Expert Comment

by:Rgonzo1971
ID: 39680074
Hi,

The only way that part of the table remains hidden that I can think of is when the table is inside a text box.

I would propose to replace the text box by a single cell table and put your data there

Regards
0
 
LVL 14

Accepted Solution

by:
Faustulus earned 500 total points
ID: 39685013
When you paste an Excel table into a Word document it is converted into a Word table. A Word table automatically splits into the next page. Therefore I conclude that there must be a peculiarity, either with your Excel sheet or the Word document or the code. Since I don't have access to the spreadsheet or the document I examined the code and found that it doesn't work. In particular, the table imported from Excel doesn't take the place of the table found within the bookmark. Instead, it is inserted before the bookmark, and if the bookmark contained a table that existing table would become appended to the new table. This doesn't look like it might be relevant to your problem, but it is the only thing I could find.
The following code will actually replace the table contained in the bookmark. I suggest you try this code and see if it still gives you the same problem.
Private Sub test()

    Dim WdApp As Word.Application
    Dim WdDoc As Word.document
    Dim Rng As Word.Range
    
    On Error Resume Next
    Set WdApp = GetObject(, "Word.Application")
    If Err.Number Then Set WdApp = CreateObject("Word.Application")
    On Error GoTo 0
    Set WdDoc = WdApp.Documents.Open("D:\My Documents\TestDoc.docx")
    
    Set Rng = WdDoc.Bookmarks("Chart").Range
    ThisWorkbook.Sheets("Customer Quotes").UsedRange.Copy
    Rng.Paste
    
    With WdDoc.Bookmarks
        .Item("Chart").Range.Rows.Delete
        .Add Name:="Chart", Range:=Rng
    End With
End Sub

Open in new window

If this doesn't provide cure it would become necessary to look at the Word document and, for good measure, as the Excel worksheet that is being pushed around.
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 39737209
I've requested that this question be deleted for the following reason:

Not enough information to confirm an answer.
0
 

Author Comment

by:DC1983
ID: 39737213
Apologies for delayed response. I'm actually overseas with the military right now on a training exercise.

I found there was a default setting in MS Word that was causing problems.

Under File: Options: Advanced: Layout Options, there was a box checked saying "Don't Break Wrapped Tables Across Pages"

As soon as this button was unchecked it began working normally. Thank you to all who responded. Your responses were very helpful in learning about VBA
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question