Solved

Inserting Page Breaks into MS Word VBA

Posted on 2013-11-26
7
1,751 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 51

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

734 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