Solved

Inserting Page Breaks into MS Word VBA

Posted on 2013-11-26
7
1,579 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

867 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now