Solved

Inserting Page Breaks into MS Word VBA

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

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 47

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

726 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