Solved

Inserting Page Breaks into MS Word VBA

Posted on 2013-11-26
7
1,521 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
Comment Utility
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 48

Expert Comment

by:Rgonzo1971
Comment Utility
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
Comment Utility
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 45

Expert Comment

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

Not enough information to confirm an answer.
0
 

Author Comment

by:DC1983
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

771 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

10 Experts available now in Live!

Get 1:1 Help Now